4

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.

6 Answers 6

3

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.

2
  • 4
    You should solve the original problem with sed '/./{s/$/|/}'
    – iruvar
    Apr 9, 2014 at 23:12
  • 3
    @1_CR, my thought too, but I had s/.$/&|/ Apr 9, 2014 at 23:46
3
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...

2

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'
1
  • Thanks for letting me know a perl solution. :) I believe perl solutions are more elegant than sed solutions.
    – Ramesh
    Apr 10, 2014 at 3:46
2

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

sed 's/\(.\)$/\1|/' file1.txt
1

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~.

0
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .