sed command to remove the first field from a '|' delimited file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command to remove the first field from a '|' delimited file
# 1  
Old 07-22-2011
sed command to remove the first field from a '|' delimited file

Hi
I have a file with fields delimited by |. I need to remove the first field from the file. I tried cut but it just extracts that field.

sample.output
abc|100|name1
cde|200|name2
efg|300|name3

Output should be
sample.output
100|name1
200|name2
300|name3

thanks
Var
# 2  
Old 07-22-2011
Code:
sed -i 's/^[^|]*|//' input.txt

# 3  
Old 07-22-2011
Quote:
var285:
"I tried cut but it just extracts that field."
The cut does work:
Code:
cut -d'|' -f2- Input_File

# 4  
Old 07-22-2011
Thank you Shell_Life and Mirni..
Mirni
For sed i am getting error when i use -i option
Code:
> sed -i 's/^[^|]*|//' FINAL.output
sed: illegal option -- i
Usage: sed [-n] [-e script] [-f source_file] [file...]

But i was able to use by redirecting it
Code:
sed 's/^[^|]*|//' FINAL.output > FINAL.output.new

But i dont want to create a temporary file. Do we have any option to edit within the same file.
Shell_life
Thanks, I was using wrong option. Is it possible to redirect the output to same file?

Thanks
Var

Last edited by radoulov; 07-22-2011 at 06:04 PM.. Reason: Code tags, please!
# 5  
Old 07-22-2011
Well, that's what -i (in-place edit) of sed does. If your version of sed doesn't support it, then temporary file can be used, just like you did. You could do without, with a (rather obscure) construct:
Code:
{ rm input.txt && sed 's/^[^|]*|//' > input.txt; } < input.txt

Same thing with cut
Code:
{ rm input.txt && sed 's/^[^|]*|//' > input.txt; } < input.txt

Or, using a subshell instead of a block:
Code:
( rm input.txt && sed 's/^[^|]*|//' > input.txt ) < input.txt

But, in my opinion, these are quite artificial, and it's much more clear doing:
Code:
sed 's/^[^|]*|//' input.txt > input.txt.tmp && mv input.txt.tmp input.txt


Please use code tags when posting code and/or sample in/output.
# 6  
Old 07-22-2011
ed can save the day! Smilie

Code:
printf %s\\n '1,$s/^[^|]*|//' w q | ed -s file

or using a heredoc
Code:
ed -s file <<'EOED'
1,$s/^[^|]*|//
w
q
EOED

---------- Post updated at 05:25 PM ---------- Previous update was at 05:21 PM ----------

Quote:
Originally Posted by var285
But i dont want to create a temporary file.
I'm curious. Why not?

Regards,
Alister
# 7  
Old 07-22-2011
Quote:
Originally Posted by alister
ed can save the day! Smilie

Code:
printf %s\\n '1,$s/^[^|]*|//' w q | ed -s file

or using a heredoc
Code:
ed -s file <<'EOED'
1,$s/^[^|]*|//
w
q
EOED

---------- Post updated at 05:25 PM ---------- Previous update was at 05:21 PM ----------



I'm curious. Why not?
I am going to use this in the C code and in this case i have to use multiple execl commands to remove the temp file etc..

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace field in the delimited file

Hi, I have the requirement similar to the one mentioned in the below thread. https://www.unix.com/unix-for-dummies-questions-and-answers/128155-search-replace-string-only-particular-column-delimited-file.html The only difference is that I need to change the field for row 1,2 and the last... (14 Replies)
Discussion started by: chetanojha
14 Replies

2. Shell Programming and Scripting

How to remove alphabets/special characters/space in the 5th field of a tab delimited file?

Thank you for 4 looking this post. We have a tab delimited file where we are facing problem in a lot of funny character. I have tried using awk but failed that is not working. In the 5th field ID which is supposed to be a integer only of that file, we are getting corrupted data as below. I... (12 Replies)
Discussion started by: Srithar
12 Replies

3. Shell Programming and Scripting

Remove Last field from a delimited file

Hi, I have a '~' delimited file and i want to remove the last field using awk. Please find the sample records below: 1428128~1~0~1100426~003~50220~005~14~0~194801~11~0~3~14~0~50419052335~0~0820652001~2~00653862 ~0~1~0~00126~1~20000110~20110423~R~ ~0~Z~1662.94~ ~002041~0045~Z~... (3 Replies)
Discussion started by: Arun Mishra
3 Replies

4. UNIX for Dummies Questions & Answers

add new 'date field' in a pipe delimited file

i need to add a new field in a pipe delimited line. the field will be the current date today. aa|a|s|w|1 as|oiy|oiy|oiy|2 given that all lines are uniformed in the number of fields i want it to look like this:\ aa|a|s|w|1|20120126 as|oiy|oiy|oiy|2|20120126 please help :) (3 Replies)
Discussion started by: kokoro
3 Replies

5. Shell Programming and Scripting

Pad zeroes first field in a Delimited file

Need help. I tried using an awk command to pad zeroes. Unfortunately, the "|" pipe delimited character is gone when I tried to write the records to another file. awk -F \| ' {$1=sprintf("%06s", $1); print $0}' $CUSTFINAL2 > $CUSTFINAL3 BEFORE "KEYRECORD"|"SA ID"|"PER ID"|"SP ID"|"ACCT... (3 Replies)
Discussion started by: johnhips
3 Replies

6. Shell Programming and Scripting

Trying to use sed to remove the value of one field from another field

I'm trying to use sed to remove the value of one field from another field. For example: cat inputfile 123|ABC|Generic_Textjoe@yahoo.com|joe@yahoo.com|DEF 456|GHI|Other_recordjohn@msn.com|john@msn.com|JKL 789|MNO|No_Email_On_This_One|smith@gmail.com|PQR I would like to remove the email... (2 Replies)
Discussion started by: bribri87
2 Replies

7. UNIX for Dummies Questions & Answers

Insert Field into a tab-delimited file

Hello, I have about 100 files in a directory with fields which are tab delimited. I would like to append the file name as the first field and it has to be done as many times as the total lines in the file. For example, myFile1.txt has the following data: 1 x y z 2 a b ... (5 Replies)
Discussion started by: Gussifinknottle
5 Replies

8. Shell Programming and Scripting

insert a field into a tab delimited file

Hello, Can someone help me to do this with awk or sed? I have a file with multiple lines, each line has many fields separated with a tab. I would like to add one more field holding 'na' in between the first and second fields. old file looks like, 1, field1 field2 field3 ... 2, field1... (7 Replies)
Discussion started by: ssshen
7 Replies

9. Shell Programming and Scripting

Count field frequency in a '|' delimited file

I have a large file with fields delimited by '|', and I want to run some analysis on it. What I want to do is count how many times each field is populated, or list the frequency of population for each field. I am in a Sun OS environment. Thanks, - CB (3 Replies)
Discussion started by: ChicagoBlues
3 Replies

10. UNIX for Dummies Questions & Answers

Replacing a field in pipe delimited TEXT File

Hi, I want to replace a field in a text delimited file with the actual number of records in the same file. HDR|ABCD|10-13-2008 to 10-19-2008.txt|10-19-2008|XYZ DTL|0|5464-1|0|02-02-2008|02-03-2008||||F||||||||| DTL|1|5464-1|1|02-02-2008|02-03-2008|1||JJJ... (3 Replies)
Discussion started by: ravi0435
3 Replies
Login or Register to Ask a Question