To add a pipe in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To add a pipe in a file
# 1  
Old 07-01-2014
To add a pipe in a file

Hi All,

how to add a pipe "|" in name field in my .dat file, below is the sample file.
name field is not case sensitive, it is mixed with upper and lower characters.
Code:
EMPLID|NAME |STID |STATUS |HRID |OIN(COLUMN NAME)

Code:
001123456One,Test|2027062|A|HIR|NPS|
001123457Expat,Two|6027062|A|HIR|NPS|
001167812shute,Clifford W|9421622|R|RET|PEN|
001203156Edmondson,Gwendolyn E|4303928|R|RET|
001224571calder,Jeanne Y|4418327|R|RET|FAP|

Expeted output like below.

Code:
001123456|One,Test|2027062|A|HIR|NPS|
001123457|Expat,Two|6027062|A|HIR|NPS|
001167812|shute,Clifford W|9421622|R|RET|PEN|
001203156|Edmondson,Gwendolyn E|4303928|R|RET|
001224571|calder,Jeanne Y|4418327|R|RET|FAP|

could any on suggest me. how to do.

Thanks,
Krupa

Moderator's Comments:
Mod Comment Please use code tags

Last edited by krupasindhu18; 07-01-2014 at 04:03 PM..
# 2  
Old 07-01-2014
Is the EMPLID a fixed size field? Always 9 characters?
# 3  
Old 07-01-2014
To add a pipe in a file

yes, it is always 9 characters .

but it would be better, if put beginning of the NAME column.

Thanks,
Krupa
# 4  
Old 07-01-2014
Try this:
Code:
awk -F\| '$1=substr($1,1,9) FS substr($1,10)' OFS=\| file

Or with sed:
Code:
sed 's/\(.........\)\(.*\)/\1|\2/' file

Code:
sed 's/\([0-9]*\)\(.*\)/\1|\2/' file


Last edited by Franklin52; 07-01-2014 at 04:29 PM..
# 5  
Old 07-01-2014
Assuming name field always starts with a character:
Code:
sed 's#[a-zA-Z]#|&#' file

# 6  
Old 07-01-2014
Does your file have that header, or not?

This should work in either case, although it may require GNU sed:
Code:
sed -E 's/^[[:digit:]]+/&|/' file

Also, note that sed won't change the input file - you'll either need to specify inplace (-i on GNU sed), or redirect the output to a temp file.
# 7  
Old 07-01-2014
Quote:
Originally Posted by Yoda
Assuming name field always starts with a character:
Code:
sed 's#[a-zA-Z]#|&#' file

Thanks a lot
could you explain this command?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort file using pipe

I am trying to create a file (epilepsy70_average.txt) and then pipe that file into a sort and save a new file. The new file is sort.txt but as of know it is blank. I can create the file in one command and then sort it in another. Is the pipe not correct? Thank you :). awk... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

How to cut a pipe delimited file and paste it with another file to form a comma separated outputfile

Hello ppl I have a requirement to split (cut in unix) a file (A.txt) which is a pipe delimited file into A1.txt and A2.txt Now I have to join (paste in unix) this A2.txt with external file A3.txt to form output file A4.txt which should be CSV (comma separated file) so that third party can... (25 Replies)
Discussion started by: etldev
25 Replies

3. Shell Programming and Scripting

How to ignore Pipe in Pipe delimited file?

Hi guys, I need to know how i can ignore Pipe '|' if Pipe is coming as a column in Pipe delimited file for eg: file 1: xx|yy|"xyz|zzz"|zzz|12... using below awk command awk 'BEGIN {FS=OFS="|" } print $3 i would get xyz But i want as : xyz|zzz to consider as whole column... (13 Replies)
Discussion started by: rohit_shinez
13 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. UNIX for Dummies Questions & Answers

Pipe binary file matches grep results to file

I am using grep to match a pattern, but the output is strange. $ grep -r -o "pattern" * Gives me: Binary file foo1 matches Binary file foo2 matches Binary file foo3 matches To find the lines before/after, I then have to use the following on each file: $ strings foo1 | grep -A1 -B1... (0 Replies)
Discussion started by: chipperuga
0 Replies

6. Shell Programming and Scripting

Convert CSV file (with double quoted strings) to pipe delimited file

Hi, could some help me convert CSV file (with double quoted strings) to pipe delimited file: here you go with the same data: 1,Friends,"$3.99 per 1,000 listings",8158here " 1,000 listings " should be a single field. Thanks, Ram (8 Replies)
Discussion started by: Ram.Math
8 Replies

7. Shell Programming and Scripting

Replace pipe with Broken Pipe

Hi All , Is there any way to replace the pipe ( | ) with the broken pipe (0xA6) in unix (1 Reply)
Discussion started by: saj
1 Replies

8. Shell Programming and Scripting

addition of the pipe in file

Hi All, I have file on AIX server CBICD312.1.2010-06-14-06.log and its cotent is ... (3 Replies)
Discussion started by: prasson_ibm
3 Replies

9. Shell Programming and Scripting

Large pipe delimited file that I need to add CR/LF every n fields

I have a large flat file with variable length fields that are pipe delimited. The file has no new line or CR/LF characters to indicate a new record. I need to parse the file and after some number of fields, I need to insert a CR/LF to start the next record. Input file ... (2 Replies)
Discussion started by: clintrpeterson
2 Replies

10. Shell Programming and Scripting

File transfer to a pipe

We were using ftp before and were able to pipe our files get a1 p1 where p1 is pipe this way we did not have to store files on our local server. Now we are using sftp2 and in sftp2 get and mget are synonymous. When I do get a1 p1 I get error p1: No such file or directory (it's looking... (1 Reply)
Discussion started by: val0822
1 Replies
Login or Register to Ask a Question