Suppose I have string as follwing:
Rajat;Harshit Srivastava;Mayank 123;5
Now i want result as following using cut command
Rajat
Harshit Srivastava
Mayank 123
5
I have tried but cut is not working on string containing spaces.
man cut would tell you:
-d, --delimiter=DELIM
use DELIM instead of TAB for field delimiter
--output-delimiter=STRING
use STRING as the output delimiter the default is to use the
input delimiter
If you insist on using cut for changing the delimiters:
$ echo "Rajat;Harshit Srivastava;Mayank 123;5" | cut -d \; --output-delimiter=\ -f 1-
Rajat Harshit Srivastava Mayank 123 5
but instead you should use sed or tr or awk for it. Try man tr, for example.
Try this
echo "Rajat;Harshit Srivastava;Mayank 123;5" | sed 's/;/ /g'
Related
I'm trying to figure out a command to parse the following file content:
Operation=GET
Type=HOME
Counters=CacheHit=0,Exception=1,Validated=0
I need to extract Exception=1 into its own line. I'm fiddling with awk, sed and grep but not making much progress. Does anyone have any tips on using any unix command to perform this?
Thanks
Since your file is close to bash syntax, there is a fun little trick you can do to make bash itself parse the file. First, use some program like tr to transform the input into a something bash can parse, and then "source" that, which will create shell variables you can expand later to get the values.
source <(tr , $'\n' < file_name_goes_here)
echo $Exception
Many ways to do this. Here is one assuming the file is called "file.txt". Grab the line you want, replace everything from the start of the line up to Except with just Except, then pull out the first field using comma as the delimiter.
$ grep Exception file.txt | sed 's/.*Except/Except/g' | cut -d, -f 1
Exception=1
If you wanted to use gawk:
$ grep Exception file.txt | sed 's/.*Except/Except/g' | gawk -F, '{print $1}'
Exception=1
or just using grep and sed:
$ grep Exception file.txt | sed 's/.*\(Exception=[0-9]*\).*/\1/g'
Exception=1
or as #sheltter reminded me:
$ egrep -o "Exception=[0-9]+" file.txt
Exception=1
No need to use a mix of commands.
awk -F, 'NR==2 {print RS$1}' RS="Exception" file
Exception=1
Here we split the line by the keyword we look for RS="Exception"
If the line has two record (only when keyword is found), then
print first field, separated using command, with Record selector.
PS This only works if you have one Exception field
I have a file setup like
TEXT1:TEXT2
Basically lines of text separated by a :
I would like all text to the right of the : gone,
so TEXT1:TEXT2 would turn into just TEXT1
Using cut
We tell cut that our field separator is a colon, -d:, and that we want to select the first field, -f1:
$ cut -d: -f1 file
TEXT1
Using sed
We tell sed to remove the first colon on a line and everything after:
$ sed 's/:.*//' file
TEXT1
Using grep
We tell grep to select the first part of each up to but not including the first colon:
$ grep -o '^[^:]*' file
TEXT1
awk -F: '{$0=$1}1' infile
TEXT1
make ":" as your delimiter and then set column1 as your record.
Below script
awk -v FS=":" '{print $1}' file
would also give you the same result.
In AWK, replace everything after the : with nothing:
$ awk 'sub(/:.*/,"",$0)' test
TEXT1
Using sed
sed -i.bkp 's/:.*//' infile.txt
This will also change the file inplace and create a backup file named infile.txt.bkp
Using grep
grep -oP '.*(?=:)' infile.txt
I have a text file with data as:
(832555,488012,0,17:31:32.541,2014-08-06 17:31:32.000,0,0,NULL,FBCD,"-6484620512517810993"etcetcetc
I want to extract the string post FBCD so my output should be:
FBCD,"-6484620512517810993"etcetc
I am able to find the position of FBCD using awk:
awk '{print substr("FBCD",1,200)}' file.txt
but I cannot extract the remaining values.
USing awk With substr
awk '{print substr($0,index($0,"FBCD"),200)' file
Using sed
sed -e 's/^.*\(FBCD.\{200\}\).*/\1/' file
If you want it till the end of the line
awk '{print substr($0,index($0,"FBCD"))' file
sed -e 's/^.*\(FBCD\)/\1/' file
Your codes: substr("FBCD",1,200)
will cut the input string from char 1 - 200. but you gave FBCD as input string, and FBCD has only length 4, that's why you got only FBCD.
In fact, grep was born to extract things, would this help you?
grep -oE 'FBCD.{1,196}' file
I have a particular sentence like this -
|/billing/gcdr/ftpdir|fw43/collectors/ANHMCA04ANT|
I would required to extract the following portion from this particular sentence -
/billing/gcdr/ftpdir
Is there any possiblity that i can do it with sed ? If yes , please help me.
along with sed and awk you can also use cut. using cut:
echo "|/billing/gcdr/ftpdir|fw43/collectors/ANHMCA04ANT|" | cut -d'|' -f2
This is not a sed solution, but if you can live with awk:
echo "|/billing/gcdr/ftpdir|fw43/collectors/ANHMCA04ANT|"| awk -F"|" '{print $2}'
will yield:
/billing/gcdr/ftpdir
Explanation of awk command:
awk -F"|" '{print $2}'
-F="|" specifies the "field separator" - it separates/groups the input line into different fields using the '|' character (rather than using whitepace by default) and then prints the second field of that line.
sed can easily do that:
sed 's/^|//;s/|.*//;'
The first sed command (s/^|//) will remove first | symbol, and the second one (s/|.*//) will remove next | and all symbols after that.
To test it run in console:
echo "|/billing/gcdr/ftpdir|fw43/collectors/ANHMCA04ANT|" | sed 's/^|//;s/|.*//;'
This might work for you:
echo '|/billing/gcdr/ftpdir|fw43/collectors/ANHMCA04ANT|' |
sed 's/^|\([^|]*\).*/\1/'
/billing/gcdr/ftpdir
I've got strange problem with cut
I wrote script, there I have row:
... | cut -d" " -f3,4 >! out
cut recieves this data (I checked it with echo)
James James 033333333 0 0.00
but I recieve empty lines in out, can somebody explain why?
You need to compress out the sequences of spaces, so that each string of spaces is replaced by a single space. The tr command's -s (squeeze) option is perfect for this:
$ ... | tr -s " " | cut -d" " -f3,4 >! out
If you want fields from a text file, awk is almost always the answer:
... | awk '{print $3" "$4}'
For example:
$ echo 'James James 033333333 0 0.00' | cut -d" " -f3,4
$ echo 'James James 033333333 0 0.00' | awk '{print $3" "$4}'
033333333 0
Cut doesn't see multiple spaces as single space, so it matches "nothingness" between spaces.
Do you get empty lines when you leave out >! out part? Ie, are you targeting correct fields?
If your input string uses fixed spacing, you might want to use cut -c 4-10,15-20 | tr -d ' ' to extract character groups 4-10 and 15-20 and remove spaces from them..
... | grep -o "[^ ]*"
will extract fields, each on separate line. Then you might head/tail them. Not sure about putting them on the same line again.