Inpu file is as below:
CMEOPT1_dump.1:1002 ZN:VTJ3J3C131
CMEOPT1_dump.1:1002 ZN:VTM4M4P123%5
CMEOPT1_dump.1:1002 ZN:VTM3M3P132%5
CMEOPT1_dump.2:1002 OZNG4
CMEOPT2_dump.3:1002 ZB:VTH4H4C132
CMEOPT2_dump.4:1002 ZN:VTK4K4P123
CMEOPT2_dump.5:1002 ZN:BOZ2Z2Z2P131%5
CMEOPT2_dump.5:1002 OZNG4
I am trying to write an awk statement should give me output as:
1002 ZN:VTJ3J3C131
1002 ZN:VTM4M4P123%5
1002 ZN:VTM3M3P132%5
1002 OZNG4
1002 ZB:VTH4H4C132
1002 ZN:VTK4K4P123
1002 ZN:BOZ2Z2Z2P131%5
1002 OZNG4
--Please help
What have you tried so far?
Many option i tried with different combination. Tried with multiple delimeter option also.. but no luck:(
If the data in the file is as it is, then below might work:
cut -f2,3 -d':' infile
thanks
Thanks for your response.. But awk is mandatory in my case.. is there a possibility in awk?
---------- Post updated at 10:54 AM ---------- Previous update was at 10:51 AM ----------
also what will happen if my 2nd field has two colons?
Thanks
$ awk '{print NF == 3 ? $2 OFS $3 : $2}' FS=":" OFS=":" file
OR
$ awk '{$0 = NF == 3 ? $2 OFS $3 : $2}1' FS=":" OFS=":" file
1002 ZN:VTJ3J3C131
1002 ZN:VTM4M4P123%5
1002 ZN:VTM3M3P132%5
1002 OZNG4
1002 ZB:VTH4H4C132
1002 ZN:VTK4K4P123
1002 ZN:BOZ2Z2Z2P131%5
1002 OZNG4
1 Like
It seems some sort of school exercise.
You could also try any of the following awk scripts:
awk '{sub(/.*:/, "", $1);print}' file
awk '{$1 = substr($1, 16);print}' file
awk '{$1 = substr($1, length($1) - 3);print}' file
One or all of them might work for you depending on how closely your actual input matches the sample input you provided.
And some more for fun:
awk '{$1=x}sub(/:/,x)' FS=: OFS=: file
awk -F'^.*:' '{print $2}' file
awk -F: 'sub($1 FS,x)' file
The latter happens to work, because the .
(any character) matches the literal dot..
sed not allowed either?
sed 's/[^:]*://' file
24ajay
February 14, 2014, 7:09am
10
awk -F":" '{print $2,$NF}'
---------- Post updated at 07:09 AM ---------- Previous update was at 07:08 AM ----------
awk -F":" '{print $2,$NF}' file
24ajay:
awk -F":" '{print $2,$NF}'
---------- Post updated at 07:09 AM ---------- Previous update was at 07:08 AM ----------
awk -F":" '{print $2,$NF}' file
Unfortunately, unlike many of the other proposals that have been made in this thread, this suggestion adds an unwanted trailing space to to the output for input lines that contain one colon character, and deletes all colons from the desired output when there are two or more colon characters on an input line. If there are more than two colons on an input line, this code drops even more of the desired output.