Appending | (pipe) to end of each line which does not have it


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Appending | (pipe) to end of each line which does not have it
# 1  
Old 12-12-2014
Appending | (pipe) to end of each line which does not have it

I have a text file in which all records end with pipe character and newline, but a few do not have a pipe at the end.

Something like this

Code:
1|John|32|US|
2|Matt|35|UK
3|Rex|36|EU|


So in the above example the second line does not have a pipe at the end

My requirement is to append a pipe to all such lines where it is missing

i tried
Code:
sed  '/|/s/.*/&|/' filename

But this one appends a pipe to all lines irrespective of the fact that it is already present or not

Please suggest

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-tags. Thank you.

Last edited by bakunin; 12-12-2014 at 04:06 AM..
# 2  
Old 12-12-2014
Quote:
Originally Posted by abhilashnair
I have a text file in which all records end with pipe character and newline, but a few do not have a pipe at the end.

Something like this

1|John|32|US|
2|Matt|35|UK
3|Rex|36|EU|
So in the above example the second line does not have a pipe at the end
My requirement is to append a pipe to all such lines where it is missing
i tried
sed '/|/s/.*/&|/' filename
But this one appends a pipe to all lines irrespective of the fact that it is already present or not
Please suggest
Hello abhilashnair,

Kindly use code tags for commands and codes which you are using in your posts as per forum rules.
Following may help you in same.
Code:
awk '{$0=$0 ~ /\|$/?$0:$0"|"} 1'  Input_file

Output will be as follows.
Code:
1|John|32|US|
2|Matt|35|UK|
3|Rex|36|EU|

Here is the link for forum rules which may help you in understanding the rules of forum too.
https://www.unix.com/misc.php?do=cfrules

Thanks,
R. Singh

Last edited by RavinderSingh13; 12-12-2014 at 03:00 AM.. Reason: Added forum rules link now
# 3  
Old 12-12-2014
Quote:
Originally Posted by abhilashnair
Code:
sed  '/|/s/.*/&|/' filename

But this one appends a pipe to all lines irrespective of the fact that it is already present or not
In fact it appends a pipe ("s/.*/&|/" means replace a whole line with itself plus a pipe) only in lines which already contain at least one pipe character ("/|/").

Try this:

Code:
sed 's/[^|]$/&|/' /path/to/file

This reads: if there is a pattern: "not-pipe, followed by line-end" ("[^|]$") found, then replace it with itself followed by a pipe. Because the line-end is not really a character this works.

I hope this helps.

bakunin
# 4  
Old 12-12-2014
Thanks, the awk command worked for me.
# 5  
Old 12-12-2014
Hi,

One more awk command if you dont mind:

Code:
awk '{if($NF!= "|")$NF=($NF"|");print}' FS=""  OFS="" file
1|John|32|US|
2|Matt|35|UK|
3|Rex|36|EU|

# 6  
Old 12-12-2014
Quote:
Originally Posted by bakunin
In fact it appends a pipe ("s/.*/&|/" means replace a whole line with itself plus a pipe) only in lines which already contain at least one pipe character ("/|/").

Try this:

Code:
sed 's/[^|]$/&|/' /path/to/file

This reads: if there is a pattern: "not-pipe, followed by line-end" ("[^|]$") found, then replace it with itself followed by a pipe. Because the line-end is not really a character this works.

I hope this helps.

bakunin
Or, using the initial approach, search for a non-| at the end, then substitute:
Code:
sed  '/[^|]$/ s/$/|/' filename

This has even got a more efficient substitution.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Appending CRLF to end of record

I need to append |\r\n (a pipe character and CRLF) at end of each record in Unix to all records where they are not already present. So first check for the presence of |\r\n and if absent append it else do nothing (3 Replies)
Discussion started by: abhilashnair
3 Replies

2. UNIX for Dummies Questions & Answers

Appending Date at the end ONLY in first line of file

Hi, My requirement is to append a date in format DDMMYYYYHHMISS at the end of first line of file which is HEADER. I am trying command sed -i '1s/.*/&<date_format>/' <file_name> Where <date_format>=`date +%m%d%Y%H%M%S` I am somehow misisng the right quotes ti get this added in above... (2 Replies)
Discussion started by: sanjaydubey2006
2 Replies

3. Shell Programming and Scripting

appending a blank line for a group of files at the end

hi, i m having a group of files starting with name 'Itemdelete<timestamp>' . my requirment is to append a blank line at the end of files ,using unix in all the Itemdelete* files with a single unix command without using scripts.can any body put some light to this requiremnt. regards Angel (4 Replies)
Discussion started by: angel12345
4 Replies

4. Shell Programming and Scripting

Appending a new field at the end in a file

can anyone tell me please ......how to append a new field at the end of a file with the help of sed or some other command in bourne shell (8 Replies)
Discussion started by: amitpta
8 Replies

5. Shell Programming and Scripting

Appending data to the end of a line

I have searched the forms and I can not find info on appending each line of one file to the same line of another file. I know that I can cat one file to another or append the 2nd file to the end of the 1st but not quite sure how to append one line of data to another. For example File 1 has ... (2 Replies)
Discussion started by: scw132
2 Replies

6. Shell Programming and Scripting

Appending the first word of each line to the end of each line

Hi Experts, Am relatively new to shell programming so would appreciate some help in this regard. I am looking at reading from a file, line by line, picking the first word of each line and appending it to the end of the line. Any suggestions? INPUT FILE - 3735051 :... (7 Replies)
Discussion started by: hj007
7 Replies

7. Shell Programming and Scripting

Filename pattern match and appending pipe

Hi, I have a directory with around 100k files and files with varying sizes(10GB files to as low as 5KB). All the files are having pipe dilimited records. I need to append 7 pipes to the end of each record, in each file whose name contains _X3_ and need to append 10 pipes to the end of each... (3 Replies)
Discussion started by: nss280
3 Replies

8. Shell Programming and Scripting

Appending string at the end of the file

Hello, I wanted to append 'XYZ' at the end of the text file. How can i do this? I searched the forums and i am not getting what i want. Any help is highly appreciated. Thanks (2 Replies)
Discussion started by: govindts
2 Replies

9. Shell Programming and Scripting

appending date at the end of the file

I have file called xx Now i want to rename this file as xxYYYYMMDD_HHMIAM.xls Here is my code.. export DATE1=`date +%Y%m%d` mv xx xx$DATE1 This code renames as xxYYYYMMDD Now how can i append HHMIAM at the end of the file? Any help is appreciated... (3 Replies)
Discussion started by: govindts
3 Replies

10. Shell Programming and Scripting

appending a line to the end of several hundred files

I have a bunch of files named publish.php within subdirs. I need to append a line at the end of each file. I thought I could do it with find and echo like this: find . -name publish.php -exec echo "<? include('path/to/file.php'); ?>" >> '{}' \; but that appends the line to a file named {}... (2 Replies)
Discussion started by: surroscape
2 Replies
Login or Register to Ask a Question