Visit Our UNIX and Linux User Community


To append Character in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To append Character in file
# 8  
Old 09-05-2013
Hello,

For fun, a sed version:
Code:
$ cat file_for_sed 
aa,"bb,rr","cc,aa",dd,ee
aa,"bb;rr","cc,aa",dd,ee
aa,bb,c,dd,ee
zz,yy,sss,ddd,ff
aa,bb,c,"dd,xx",ee
aa,bb,c,"dd;xx",ee

Code:
$ cat file.sed 
s/"\([^,]*\)\(,\)\{0,1\}\([^"]*\)"/@"@\1@@\2@@\3@"@/g
s/@@,@@/@@_@@/g
s/^\(\([^,]*\,\)\{3\}\(@"@\)\{0,1\}\)/\1LQ/
s/@@_@@/,/g
s/@@@@//g
s/@"@/"/g

Code:
$ sed -f file.sed file_for_sed 
aa,"bb,rr","cc,aa",LQdd,ee
aa,"bb;rr","cc,aa",LQdd,ee
aa,bb,c,LQdd,ee
zz,yy,sss,LQddd,ff
aa,bb,c,"LQdd,xx",ee
aa,bb,c,"LQdd;xx",ee

Regards.
# 9  
Old 09-05-2013
Code:
echo 'aa,"bb",c,dd,ee' | perl -ne '/(.+,.+,.+,)(.*)/ and print "$1LQ$2"'


Last edited by Scott; 09-05-2013 at 09:15 AM.. Reason: Code tags
# 10  
Old 09-05-2013
Hi,

This solution don't work, just add 'LQ' to last field.

Regards.
# 11  
Old 09-05-2013
@disedorgue
Sed solution does not work with this line:
aa,"bb,rr,ff","cc,aa",dd,ee gives aa,"bb,rr,ff","LQcc,aa",dd,ee
correct
Code:
aa,"bb,rr,ff","cc,aa",LQdd,ee



Fixed my solution to handle multiple , in one field
Code:
cat file
aa,"bb,rr,ff,ss","cc,aa",dd,ee
aa,"bb,rr,ff","cc,aa",dd,ee
aa,"bb,rr","cc,aa",dd,ee
aa,bb,c,dd,ee
zz,yy,sss,ddd,ff
aa,bb,c,"dd,xx",ee

Code:
awk -F, '
	{
	for (i=1;i<=NF;i++)
		if ($i!~/^"|"$/ && !p) {a[++f]=$i}
		else if ($i~/^"/) {a[++f]=$i;p=1}
		else if ($i~/"$/) {a[f]=a[f]","$i;p=0}
		else {a[f]=a[f]","$i}
	}
	{a[4]=(a[4]~/^\"/)?"\"LQ"substr(a[4],2):"LQ"a[4]
	for (i=1;i<f;i++) {printf "%s,",a[i]}
	print a[f]
	delete a;
	f=0}
	' file
aa,"bb,rr,ff,ss","cc,aa",LQdd,ee
aa,"bb,rr,ff","cc,aa",LQdd,ee
aa,"bb,rr","cc,aa",LQdd,ee
aa,bb,c,LQdd,ee
zz,yy,sss,LQddd,ff
aa,bb,c,"LQdd,xx",ee

This User Gave Thanks to Jotne For This Post:
# 12  
Old 09-05-2013
Ok, here is a fixed version:
Code:
$ cat file.sed 
:deb
s/@@,@@/@@_@@/g
s/\("\)\([^,]\+\)\(,\)\([^"]\+"\)/\1\2@@\3@@\4/
tdeb
s/^\(\([^,]*\,\)\{3\}\("\)\{0,1\}\)/\1LQ/
s/@@_@@/,/g

Code:
$ cat file_for_sed 
aa,"bb,rr","cc,aa",dd,ee
aa,"bb;rr","cc,aa",dd,ee
aa,bb,c,dd,ee
zz,yy,sss,ddd,ff
aa,bb,c,"dd,xx",ee
aa,bb,c,"dd;xx",ee
aa,"bb,rr,ff","cc,aa",dd,ee
aa,"bb,rr,ff,ss","cc,aa",dd,ee

Code:
$ sed -f file.sed file_for_sed 
aa,"bb,rr","cc,aa",LQdd,ee
aa,"bb;rr","cc,aa",LQdd,ee
aa,bb,c,LQdd,ee
zz,yy,sss,LQddd,ff
aa,bb,c,"LQdd,xx",ee
aa,bb,c,"LQdd;xx",ee
aa,"bb,rr,ff","cc,aa",LQdd,ee
aa,"bb,rr,ff,ss","cc,aa",LQdd,ee

Regards.
This User Gave Thanks to disedorgue For This Post:
# 13  
Old 09-05-2013
I must say I'm a little disappointed in Lorance Stinson's CSV Parser I've had to tweak it a fair bit to get the csv_create working correctly but he does say it's a Beta version so some tweaking was expected (the csv_parse worked without a hitch).

So if you ever get embedded newlines in your fields, the attached version supports them eg:

Code:
$ cat infile
aa,bb,c,dd,ee
zz,yy,sss,ddd,ff
aa,bb,c,"dd,xx",ee
aaa,"and now,
for ""something"" interesting",d,e,f

$ awk -f add_LQ.awk.txt infile
aa,bb,c,LQdd,ee
zz,yy,sss,LQddd,ff
aa,bb,c,"LQdd,xx",ee
aaa,"and now,
for ""something"" interesting",d,LQe,f

Your CSV's may never be this complex but it's nice to have around.

Last edited by Chubler_XL; 09-05-2013 at 06:05 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #553
Difficulty: Medium
The standard input (stdin for short), standard output (stdout) and standard error (stderr) are three standard communication streams that are defined by most computer systems.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding character and append last element of second column

Hi, could you help me in processing this file under bash? I need to add some text to the first line and then append the last element of the second columns. The input file is tab separated while the output should be space separated. input file is 1.00E-02 2.00E-02 4.465E+17 2.00E-02 3.00E-02... (4 Replies)
Discussion started by: f_o_555
4 Replies

2. UNIX for Beginners Questions & Answers

Append each line based upon the character size

I have a huge file which contains multiple lines. It need to check whether character length is not more than 255 each line. If its not then it should remove the character up to column. I have described in the output below. If its more than that the next line should start with call but if the... (1 Reply)
Discussion started by: JoshvaPeter
1 Replies

3. Shell Programming and Scripting

Find string in a file and append character

Hi Experts, Is there a way to find a string in a file then append a character to that string then save the file or save to another file. Here is an example. >cat test.txt NULL NULL NULL 9,800.00 NULL 1,234,567.01 I want to find all NON NULL String and add a dollar sign to those... (9 Replies)
Discussion started by: brichigo
9 Replies

4. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

5. Shell Programming and Scripting

How do I append a ^M to the end of each 129 character string

Hello all, I have a stumper of a problem. I am trying to append a ^M or "newline" to the end of each 129 character string in a huge file in unix. Each string starts with A00. I am trying to get the file to go from... A00vswjdv1 Test Junk Junk A00vswjdv2 Test Junk Junk ... (6 Replies)
Discussion started by: Captain
6 Replies

6. Shell Programming and Scripting

Sed append newline character

Hi All, I am new to Shell scripting.. I have a task to parse the text file into csv format. more then half the things has done. But the problem is when I use the sed command in shell script. it appends newline character at the end of the line. and so when I open the file in CSV it's format... (3 Replies)
Discussion started by: Gaurang033
3 Replies

7. Shell Programming and Scripting

append a character at end of each line of a file

Hi, i want to append a character '|' at end of each line of a file abc.txt. for example if the file abc.txt conatins: a|b|c 1|2|33 w|2|11 i want result file xyz.txt a|b|c| 1|2|33| w|2|11| I know this is simple but sumhow i am not able to reach end of line. its urgent, thanks for... (4 Replies)
Discussion started by: muaz
4 Replies

8. UNIX for Dummies Questions & Answers

How to Replace,Sort,and Append Character one script

Hi all i am very new to shell scripting,hope u guys can help i need to replace,sort and append character for the file that look like this: 1007032811010001000100000001X700026930409 1007032811010001000200000002X700026930409 1007032711020001000300000003X700026930409... (2 Replies)
Discussion started by: ashikin_8119
2 Replies

9. Programming

append character to a file

Hi, fputc() will overwrite from the beginning of the target file. How would I append characters to the target file's end instead? Thanks. (3 Replies)
Discussion started by: enuenu
3 Replies

10. Shell Programming and Scripting

Using SED to append character to each line

Hey - my first post here, and I'm a total SED newb. I've looked around for previous help on this, but have so far been unsuccessful. I have a program (AMStracker for OS X) that outputs data in the terminal. Output is in this form: . . . 3 0 -75 3 0 -76 3 0 -77 ... (4 Replies)
Discussion started by: c0nn0r
4 Replies

Featured Tech Videos