awk one line, change \n to sth in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk one line, change \n to sth in a file
# 1  
Old 06-08-2010
awk one line, change \n to sth in a file

Hi Everyone,
Code:
cat 1.txt
aaa
bbb
ccc

outout will be
Code:
cat 2.txt
,,aaa,,bbb,ccc,,

means change "\n" to ",,", and add ",," into the beginging and ending.
right now i am using perl while to open and read the file, then split \t, feel not nice. please advice.

and i hear using perl is faster than awk, when doing some replace string in a file, is it?

Thanks

Last edited by jimmy_y; 06-08-2010 at 01:18 PM..
# 2  
Old 06-08-2010
Maybe you're looking for something like this?
Code:
perl -ane '$_ =~ s/^/,,/g if $. == 1; $_ =~ s/\n/,,/g; print' 1.txt > 2.txt

This User Gave Thanks to pseudocoder For This Post:
# 3  
Old 06-08-2010
Code:
awk 'BEGIN{ORS=",,"}NR==1{$0=",,"$0;print;next}1' infile

This User Gave Thanks to bartus11 For This Post:
# 4  
Old 06-08-2010
Code:
awk -v ORS=",," 'BEGIN {printf ORS}1' file

This User Gave Thanks to Scott For This Post:
# 5  
Old 06-08-2010
Thanks everyone, your code is working nice.

Another question, if instead of ",,", if i want to if there is a space, i want to replace it with "" (means none).
for exmaple:

Code:
cat 1.txt
 aaa
bbb 
 ccc

output
Code:
cat 2.txt
,,aaa,,bbb,ccc,,

Thanks
# 6  
Old 06-08-2010
Added gsub statement to scottn code

Code:
awk -v ORS=",," 'BEGIN {printf ORS} {gsub(" ","")} 1' file

This User Gave Thanks to anbu23 For This Post:
# 7  
Old 06-08-2010
Quote:
Originally Posted by anbu23
Added gsub statement to scottn code

Code:
awk -v ORS=",," 'BEGIN {printf ORS} {gsub(" ","")} 1' file

thanks it works Smilie, just would like to know the "1" means? (1' file), the "1". thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to change file section into each line?

Hi Gurus, I have below file which has different sections, need to move the sections to beginning of the each record. original file aaa bbb ccc ddd eee fff output file. aaa bbb ccc ddd eee fff (6 Replies)
Discussion started by: green_k
6 Replies

2. UNIX for Beginners Questions & Answers

How to change name of the file with first line of the file which has some unwanted text in it?

I have a log file, which i have divided into 14 files using csplit, the file looks like below test-000000 test-000001 #and so on until 14 now I want all the 14 files generated to be renamed as the some part of test in first line of the file how can i eliminate the unwanted text? sample... (5 Replies)
Discussion started by: Sekhar419
5 Replies

3. Shell Programming and Scripting

awk to change comma separated line to horizontal

I am trying to change a file that looks like this: file, announcement,date, server, server01, server02, server06, file04, rec01, rec04, rec03... etc into a vertical file like this: file announcement date server server01 server02 server06 The file does not have to be sorted... (5 Replies)
Discussion started by: newbie2010
5 Replies

4. Shell Programming and Scripting

Replacing last line with awk and change the file name

Hi Guys, I am having a set of date format files files where I am performing the below set of operations in the files . I Need to replace the last line value with specific date which is a pipe delimited file. for egf1_20140101.txt aa|aus|20140101|yy bb|nz|20140101|yy . .... (19 Replies)
Discussion started by: rohit_shinez
19 Replies

5. Shell Programming and Scripting

Need script to change a line in file....

Hello all, I have a line of code in a file that I need to change in the /etc/sysconfig/kdump file presently the line reads: KDUMP_COMMANDLINE_APPEND="irqpoll nr_cpus=1 reset_devices cgroup_disable=memory mce=off" what I need to do is put a comment out the 1st line and repeat it, and... (5 Replies)
Discussion started by: gartie
5 Replies

6. Shell Programming and Scripting

Change in last line of each part of a file

Hi, I have a input files as below. Sample of one input file type mtg_key = record decimal("\344") id; string("\344") ct_cd; string("\344") st_cd; end; type psl_key = record decimal("\344") id; utf8 string("\344") st_cd; end; type amk_fields = record ... (6 Replies)
Discussion started by: adgangwar
6 Replies

7. Shell Programming and Scripting

Add sth to end of each line, but only for specific files

Hi. I have a list with files, and I would like to add a variable to the end of each line of each file in this list. The files are in a folder together with a large number of other files which I don't want to change. My code is: for file in 'cat ../list' do sed 's/$/\/R:_0/' $file >>... (4 Replies)
Discussion started by: Bloomy
4 Replies

8. Shell Programming and Scripting

how to change one line in a file?

Suppose I have a file. The content of the file is: Fwf33 #enable_a STH. fwqfw egvega What I want to do is: if "STH" = "YES", do not change it, otherwise, change it to "YES". e.g. if “enable_a ERR” change it to be “enable_a YES” if “enable_a YES” do not change the file. ... (2 Replies)
Discussion started by: lilili07
2 Replies

9. Shell Programming and Scripting

Script to change file contents line by line

Hi, I'm struggling to write a script to do the following, -will go through each line in the file -in a specific character positions, changes the value to a new value -These character positions are fixed througout the file ----------------------- e.g.: file1.sh will have the following 3... (4 Replies)
Discussion started by: vini99
4 Replies

10. UNIX for Dummies Questions & Answers

Change Specific Line of a File

Hi everyone, I am attempting to do something that should be very simple. How do I replace a specific line of a file with different text, and then save that file to its original name? I believe I want to use the sed command with the c option, but I after trying many times, I can't get the right... (10 Replies)
Discussion started by: msb65
10 Replies
Login or Register to Ask a Question