Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

After some processing, I have my input file like below.

file1.txt

12345|john|student
43321|jack|professor

78965|alex|lecturer

I need to process the above file further and so I need a line breaker at the end of the line too. Currently, I can accomplish it as below.

sed 's/$/|/' file1.txt

The above command results in the output as,

12345|john|student|
43321|jack|professor|
|
78965|alex|lecturer|

As we can see, the | is appended to blank lines too. When I tried to remove the | character again from blank lines using below command,

sed 's/|//g' file1.txt

The | character is getting deleted everywhere. How can I delete only the | in blank lines? I need to keep the blank line also.

share|improve this question
add comment

6 Answers 6

up vote 1 down vote accepted

From your original, you can add | to lines that is not blank:

Using sed:

sed -e '/^$/!s/$/|/' file

Using perl:

perl -lpe 's/$/|/ if length' file

If you want to remove | from lines that is blank (in second version):

perl -lpe 's/\|// if length == 1'
share|improve this answer
    
Thanks for letting me know a perl solution. :) I believe perl solutions are more elegant than sed solutions. –  Ramesh Apr 10 at 3:46
add comment

I was able to do it using the below command.

sed 's/^|//' file1.txt

Basically, I check only the first character using ^ and then if it is |, I am deleting it.

share|improve this answer
4  
You should solve the original problem with sed '/./{s/$/|/}' –  1_CR Apr 9 at 23:12
1  
+1 for self research and learning. –  Graeme Apr 9 at 23:42
3  
@1_CR, my thought too, but I had s/.$/&|/ –  glenn jackman Apr 9 at 23:46
add comment

Only append a | to a line with at least one character:

sed 's/\(.\)$/\1|/' file1.txt
share|improve this answer
add comment
sed '/../!d;a\ ' file

This would serve close to the same purpose as the one below; it would delete any line not containing 2 or more characters, and appending a blank to all of the rest.

This gives me a file kind of like your issue:

printf '12345|john|student\n|\n|\n|\n%.0b' `seq 1 10` >|./file1.txt

It looks like this:

12345|john|student
|
|
|

But repeated 10 times.

Then this:

sed -n '/[^|].*/s//&|\n/p' ./file1.txt

Gives me:

12345|john|student|

12345|john|student|

12345|john|student|

12345|john|student|

12345|john|student|

12345|john|student|

12345|john|student|

12345|john|student|

12345|john|student|

12345|john|student|

Sorry it took me a little while to do it right; I need to stop writing these answers on my phone...

share|improve this answer
add comment

With sed:

sed -i~ -re 's/^\|$//' file

Searches ^ begin of line a pipe \| and end of line $. // deletes the character. Edit file in place with -i~ and create a backup file~.

share|improve this answer
    
Thanks for letting me know this solution. :) –  Ramesh Apr 10 at 3:45
add comment

Starting from the command in question, you can remove the option g (recursive) and add the hat character ^ before the pipe to check only the first character:

sed 's/^|//' file1.txt
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.