awil
June 14, 2014, 3:03am
1
Hello,
I have some problem about sed command. I have data which showed in below :
M001 ndle1
M002|bfn|n|bfn|bf ndle1
M003|bfn|n|bf|bf ndle1
M004|bfn|n|bf middle1
M005|bfn|n|bfn|bf middle1
M006|bnf|n|bfn|bf middle1
M007|fn|q|n|bf middle1
The expected output :
M001 ndle1
M002 ndle1
M003 ndle1
M004 middle1
M005 middle1
M006 middle1
M007 middle1
It looks like easy, but I can't solve in completely because I have a-z with | symbol. Normally, I used this code :
sed -e "s/|bfn*//g" -e "s/|n//g" -e "s/|fn//g" -e "s/|q//g" -e "s/|bnf//g" inputfile
Could you please suggest me?
Thanks in advance.
clx
June 14, 2014, 3:12am
2
If the first field length is fixed (4 char long alphanumeric with uppercase), one way could be
sed 's/^\([A-Z0-9]\{4\}\).* *\(.*\)/\1 \2/' file
If you are not sure about the length, the awk
version might come handy
awk -F "[ |]" '{print $1,$NF}' file
1 Like
Try using the inverse of the spacing characters:
sed 's/|[^[:blank:]]*//' file
2 Likes
awil
June 14, 2014, 4:26am
4
clx:
If the first field length is fixed (4 char long alphanumeric with uppercase), one way could be
sed 's/^\([A-Z0-9]\{4\}\).* *\(.*\)/\1 \2/' file
If you are not sure about the length, the awk
version might come handy
awk -F "[ |]" '{print $1,$NF}' file
Thank you very much. for your suggestion. The first field length is not fixed, they vary in other length. I tried to use this code, but it do not give me in expected output.
---------- Post updated at 03:26 PM ---------- Previous update was at 03:24 PM ----------
Thank you very much for your suggestion. I tried to use this code, I can get the expected output.