awk print in one line after reading textfile with paragraphs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk print in one line after reading textfile with paragraphs
# 1  
Old 11-09-2014
awk print in one line after reading textfile with paragraphs

Hello everybody

I have a text file which has the following format:

Code:
nmm  "text20140601.033954text" "text" "text"
"text" , ... , "text"
"text" , ... , Lat  36.3247  Lon 16.0588 Depth 8
"text", ... , "text"
"text", ...,  CovXX 1.65 CovYY 2.32 CovZZ 1.2
"text" , ..., "text

nmm  "text20140604.033935text" "text" "text"
"text" , ... , "text"
"text" , ... , Lat  36.3000  Lon 16.0500 Depth 6
"text", ... , "text"
"text", ...,  CovXX 1.50 CovYY  1.10 CovZZ 1.4
"text" , ..., "text
...

(where "text" is random words or numbers)
what I want is to get the important fields of each paragraph and print them in one line (one line for each paragraph).
The desired format is :
Code:
20140601033954 36.3247 16.0588 8 1.65 2.32 1.2
20140604033935 36.3000 16.0500 6 1.50 1.10 1.4

I have tried with
Code:
awk '$1=="nmm" {print substr($2,10,8) substr($2,19,7)}   $9 ~/Lat/   {print $10, $12, $14 } $8 ~/CovXX/ {print $3, $5, $7)}' text.txt

but I get the right results in different lines.
Thank you in advance for your time and help.
# 2  
Old 11-09-2014
Got Perl?

Code:
perl -00 -wne '@values = $_ =~ /(\d+\.?\d+)/g and print "@values\n"' file.txt

This User Gave Thanks to Aia For This Post:
# 3  
Old 11-09-2014
Use printf "%s",string instead of print string!

Last edited by MadeInGermany; 11-09-2014 at 02:42 PM.. Reason: comma
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 11-09-2014
Info
Quote:
Don't fall into the trap of using a printf when a simple print would do. The print is more efficient and less error prone.
# 5  
Old 11-09-2014
With all the irregularities of spaces and commas between your meaningful fields, it might be best to use awk versatile field separator definition. Try:
Code:
awk     '$1=="nmm"              {gsub (/\"*text\"*|\./,"", $2); printf "%s ", $2}  
         $3 ~/Lat|CovXX/        {printf "%s %s %s ", $4, $6, $8}
         /^$/                   {printf "\n"}
         END                    {printf "\n"}
        ' FS="[, ]*" file
20140601033954 36.3247 16.0588 8 1.65 2.32 1.2
20140604033935 36.3000 16.0500 6 1.50 1.10 1.4

This User Gave Thanks to RudiC For This Post:
# 6  
Old 11-09-2014
A quick alternative is to run your script through:
Code:
| paste -d " " - - -


Last edited by Scrutinizer; 11-09-2014 at 04:01 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 11-09-2014
Thank you all for your replies. And special thanks to Scrutinizer.

I have tried printf BUT it prints again the result in different line.

The awk solution that i had written works fine but the results were repeatedly printed in 3 different lines for each paragraph. I just wanted the output to be printed in 1 line for each paragraph.

To sum up, the simplest and best solution for me was Scrutinizer 's by adding his code at the end of mine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. Shell Programming and Scripting

How to separate sorte different characters from one textfile and copy them in a new textfile?

My first post, so don't kill me :) Say i open some textfile with some example like this. on the table are handy, bread and wine Now i know exactly what is in and i want to separate and sorted it in terminal to an existing file with another 2 existing lines in like this: table plane ... (3 Replies)
Discussion started by: schwatter
3 Replies

3. Shell Programming and Scripting

awk to print the line that matches and the next if line is wrapped

I have a file and when I match the word "initiators" in the first column I need to be able to print the rest of the columns in that row. This is fine for the most part but on occasion the "initiators" line gets wrapped to the next line. Here is a sample of the file. caw-enabled ... (3 Replies)
Discussion started by: kieranfoley
3 Replies

4. Shell Programming and Scripting

Reading ls -l output line by line awk the user name and su user to run commands

Using ksh on AIX what I am trying to do is to read the ls -l output from a file in a do while loop line by line. Extract the user name(3rd field) and the directory/file name(9th field) using awk and save them into variables. su -c to the user and change directory/file permisions to 777. Script I... (13 Replies)
Discussion started by: zubairom
13 Replies

5. Shell Programming and Scripting

awk script -print line when $2 > $2 of previous line

Hi all, From a while loop I am reading a sorted file where I want to print only the lines that have $1 match and $2 only when the difference from $2 from the previous line is > 30. Input would be like ... AN237 010 193019 0502 1 CSU Amoxycillin AN237 080 ... (2 Replies)
Discussion started by: gafoleyo73
2 Replies

6. Web Development

Wrap one line paragraphs in string with h tags

Hi, I have text (string) like this: Blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah. Blah blah blah blah Blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah... (5 Replies)
Discussion started by: LukasB
5 Replies

7. Shell Programming and Scripting

awk: reading into an array and then print the value corresponding to index

I am beginner in awk awk 'BEGIN{for(i=1;(getline<"opnoise")>0;i++) arr=$1}{print arr}' In the above script, opnoise is a file, I am reading it into an array and then printing the value corresponding to index 20. Well this is not my real objective, but I have posted this example to describe... (19 Replies)
Discussion started by: akshaykr2
19 Replies

8. Shell Programming and Scripting

reading a file inside awk and processing line by line

Hi Sorry to multipost. I am opening the new thread because the earlier threads head was misleading to my current doubt. and i am stuck. list=`cat /u/Test/programs`; psg "ServTest" | awk -v listawk=$list '{ cmd_name=($5 ~ /^/)? $9:$8 for(pgmname in listawk) ... (6 Replies)
Discussion started by: Anteus
6 Replies

9. Shell Programming and Scripting

cut a string in a textfile line per line

i need to cut the string in a textfile but each line has a specific way of cutting it (different lengths) i have a for loop that gets the string line per line, then each line has to be compared: for x in `cat tmp2.txt`; do if; then echo 'BAC' elif ... (6 Replies)
Discussion started by: izuma
6 Replies

10. Shell Programming and Scripting

print any required line by its line no using awk and its NR variable

how to print any required line by its line no using awk and its NR variable for eg: ------------ 121343 adfdafd 21213sds dafadfe432 adf.adf%adf --------------- requied o/p if give num=3 it print: 21213sds -------------------------------------- (2 Replies)
Discussion started by: RahulJoshi
2 Replies
Login or Register to Ask a Question