AWK scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK scripting
# 8  
Old 12-01-2008
Thanks Franklin52, the code you gave works just fine, except there is one problem. I want it to make a new paragraph when tab is used as well.

So if the input file is:

Code:
First line of the second paragraph.
Second line of the second paragraph.
     Third as well.

The output is:

Code:
First line of the second paragraph. Second line of the second paragraph.
     
Third as well.

I tried adding "\t" to RS in addition to the "\n\n". Is that the right thing to do anyway? Agaiss it isn't, cause if I replaced the "\n\n" with "\t" then it should have made a new paragraph to my mind, but it didn't. So any further assistance is greatly appreciated.

Thanks in advance!

Last edited by Muki101; 12-01-2008 at 06:59 AM..
# 9  
Old 12-01-2008
You can left the awk code unaltered. Translate the tabs to newlines with tr and pipe the output to the awk command:

Code:
tr '\t' '\n' < file | awk 'BEGIN {RS=ORS="\n\n"} $1=$1'

This is what I get:

Code:
$ cat file
    The first line.
Second line.

First line of the second paragraph.
Second line of the second paragraph.
    Third as well.
$
$ tr '\t' '\n' < file | awk 'BEGIN {RS=ORS="\n\n"} $1=$1'
The first line. Second line.

First line of the second paragraph. Second line of the second paragraph.

Third as well

Regards
# 10  
Old 12-01-2008
Can someone explain the '$1=$1' part of the script? What exactly is it doing?
# 11  
Old 12-01-2008
Quote:
Originally Posted by Celos
Can someone explain the '$1=$1' part of the script? What exactly is it doing?
A trick to force awk to remove whitespaces ( and non record separators) and rearrange the line in the buffer ($0) with the new ORS.

Regards
# 12  
Old 12-02-2008
Hi i think perl is a little bit easier than awk

Code:
$/="\n\n";
open FH,"<a.txt";
while(<FH>){
	tr/\n//d;
	print $_,"\n";
}

# 13  
Old 12-02-2008
Thanks alot Franklin52! Works just fine Smilie

But I was wondering whether it`s possible to do it entirely int AWK?
# 14  
Old 12-02-2008
Quote:
Originally Posted by Muki101
Thanks alot Franklin52! Works just fine Smilie

But I was wondering whether it`s possible to do it entirely int AWK?
You already have it entirely in AWK Smilie
Just remove the tr part:

Code:
awk 'BEGIN{RS=ORS="\n\n"}$1=$1' infile

Edit: Consider that most AWK implementations do not support multiple characters for RS.
If the AWK code provided by Franklin52 is working for you, you should be using GNU AWK or tawk.

Or, if Perl is acceptable:

Code:
perl -00ple'tr/\t//d;tr/\n/ /' infile


Last edited by radoulov; 12-02-2008 at 01:24 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

New at scripting awk with variable

I am trying to write a shell script that will add a date to and awk command from the command prompt. here is the line I am having difficulties. zgrep -i playback $dir/$1-*-errors.out.gz |cut -d '|' -f 1,11,12,15 | awk -v start=${start} -v end=${end} -F '|' '{$1>=start && $1 <=end} {print $2... (7 Replies)
Discussion started by: infinity0880
7 Replies

2. Shell Programming and Scripting

awk Scripting

Hey guys, I want to get all the columns in this input file tab-delimited, i need to get the column send them to a variable. From there i could print them in shuffle and pick and select columns i want. Here is the input sample 2013/08/05 06:50:38:067 MINOR SiteScope ... (9 Replies)
Discussion started by: ryandegreat25
9 Replies

3. Shell Programming and Scripting

Need help in awk scripting

Hi I am beginner of shell/AWK scripting , can you please help me in select particular column and column between two pattern from a multiple column file. file1.txt number status date1 date2 description category ... (7 Replies)
Discussion started by: vijay_rajni
7 Replies

4. Shell Programming and Scripting

Need help with awk scripting

hi all, I am working on awk scripting.I have created two awk files and now have a requirement of replacing the contents of first file with some contents of second file. Please find below the two files created.File1 has 3 records and File2 has 4 records. cat File1 111,0165,CB21031251,0165,... (3 Replies)
Discussion started by: csrohit
3 Replies

5. Shell Programming and Scripting

Help with AWK and Scripting!

Hi, This is the first time I am working with awk and I am not familiar with any commands in it. But I managed to do most of my work just left with one more. Needing your help! I have to extract only the matrix (written within ) from a text file. For example: 1JTJ_0006_ACGC_NPNP_A_12_15.pdb ... (17 Replies)
Discussion started by: SriJit
17 Replies

6. Shell Programming and Scripting

Need help with awk scripting.

Hi, i am newbie to this site and hope to learn but problem is s but need help urgently. Plz pm me if you are good at this. Help will be appreciated. (11 Replies)
Discussion started by: Rookie80
11 Replies

7. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

8. Shell Programming and Scripting

awk scripting

Hi I have 2 files of large size( 10 Miilions) , i want to join 2 files based on some condition . for this taking lot of time and 100 cpu .i want to iterate the based on some value (like 1 lakh) I put the 2 files in the associative arrays . if the array limit reaches the 1 lach join the with... (2 Replies)
Discussion started by: kiranmosarla
2 Replies

9. Shell Programming and Scripting

Scripting via awk

Hi, I am trying to understand what is happening here, new to scripting: I have a couple of these, but if I knew what was going on in one I can figure out the rest: awk '/rpc-100083/ { $2 = "enable -r" } $3 ~ /.NOS99dtlogin/ { $t = $2; $2 = $3; $3 = $t } { print }' /var/svc/profile/upgrade... (2 Replies)
Discussion started by: ulemsee
2 Replies

10. UNIX for Dummies Questions & Answers

Awk scripting

Hi, I'm new to unix and i am kind of familiar with the basic commands. can anyone suggest some good books especially for AWK scripting and SHELL scripting thanks, Hari (2 Replies)
Discussion started by: rharee
2 Replies
Login or Register to Ask a Question