sed command in multicharacter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command in multicharacter
# 1  
Old 06-14-2014
sed command in multicharacter

Hello,

I have some problem about sed command. I have data which showed in below :
Code:
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 :
Code:
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 :
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.

Last edited by Scrutinizer; 06-14-2014 at 04:12 AM.. Reason: Changed some Quote tags to code tags
# 2  
Old 06-14-2014
If the first field length is fixed (4 char long alphanumeric with uppercase), one way could be

Code:
sed 's/^\([A-Z0-9]\{4\}\).*  *\(.*\)/\1 \2/' file

If you are not sure about the length, the awk version might come handy

Code:
awk -F "[ |]" '{print $1,$NF}' file

This User Gave Thanks to clx For This Post:
# 3  
Old 06-14-2014
Try using the inverse of the spacing characters:
Code:
sed 's/|[^[:blank:]]*//' file

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 4  
Old 06-14-2014
Quote:
Originally Posted by clx
If the first field length is fixed (4 char long alphanumeric with uppercase), one way could be

Code:
sed 's/^\([A-Z0-9]\{4\}\).*  *\(.*\)/\1 \2/' file

If you are not sure about the length, the awk version might come handy

Code:
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 ----------

Quote:
Originally Posted by Scrutinizer
Try using the inverse of the spacing characters:
Code:
sed 's/|[^[:blank:]]*//' file

Thank you very much for your suggestion. I tried to use this code, I can get the expected output.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Output of sed command to another sed command

Hi All, I'm relatively new to Unix scripting and am trying to get my head around piping. I'm trying to take a header record from one file and prepend it to another file. I've done this by creating several temp files but i'm wondering if there is a cleaner way to do this. I'm thinking... (10 Replies)
Discussion started by: BigCroyd
10 Replies

2. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

3. Shell Programming and Scripting

sed returns error "sed: -e expression #1, char 18: unterminated `s' command"

Hello All, I have something like below LDC100/rel/prod/libinactrl.a LAA2000/rel/prod/libinactrl.a I want to remove till first forward slash that is outputshould be as below rel/prod/libinactrl.a rel/prod/libinactrl.a How can I do that ??? (8 Replies)
Discussion started by: anand.shah
8 Replies

4. UNIX for Dummies Questions & Answers

sed command

I dont understand what happening in this command sed 's/.*Interface=\(.*\)/\1/' Kindly explain the significance of each symbol....and what exactly it does....:confused: Please use and tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks. (4 Replies)
Discussion started by: xerox
4 Replies

5. UNIX for Dummies Questions & Answers

sed insert command and variable expansion/command substitution

I know this script is crummy, but I was just messing around.. how do I get sed's insert command to allow variable expansion to show the filename? #!/bin/bash filename=`echo $0` /usr/bin/sed '/#include/ { i\ the filename is `$filename` }' $1 exit 0 (8 Replies)
Discussion started by: glev2005
8 Replies

6. Shell Programming and Scripting

Convert Sed command to perl command

Hello, Can any perl experts help me convert my sed string to perl. I am unsuccessful with this. I have to remove this string from html files OAS_AD('Top'); I have come up with this. However the requirement is in perl. for find in $(find . -type f -name "file1.html") ; do cat $find |... (2 Replies)
Discussion started by: abacus
2 Replies

7. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

8. Shell Programming and Scripting

Multiline read with multicharacter record seperator

I have a file like the below: Start <</NumCopies 0001>> 0223 098 et(5926)sh 0223 098 mt(5926)sh End Start <</NumCopies 0001>> 0224 098 et(5926)sh 0224 098 mt(5926)sh End This file needs to be split to seperate files. Each of the seperate file will need to... (3 Replies)
Discussion started by: pt14
3 Replies

9. Shell Programming and Scripting

awk/sed Command : Parse parameter file / send the lines to the ksh export command

Sorry for the duplicate thread this one is similar to the one in https://www.unix.com/shell-programming-scripting/88132-awk-sed-script-read-values-parameter-files.html#post302255121 Since there were no responses on the parent thread since it got resolved partially i thought to open the new... (4 Replies)
Discussion started by: rajan_san
4 Replies

10. UNIX for Dummies Questions & Answers

Help in using sed command

Hi, I'm using this command to get the year part from the filename result_20070809.txt ls | sed -e 's/^result_\(....\)\(..\)\(..\).txt$/\1/' My folder contains some other files also : test.sh ,test2.sh etc. Now i expect this command to generate only 2007 as output but it is printing:... (3 Replies)
Discussion started by: amitesh116
3 Replies
Login or Register to Ask a Question