Help with txt formatting using AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with txt formatting using AWK
# 1  
Old 08-04-2009
Help with txt formatting using AWK

Hi,
Ive used unix.com to help learn the basics of AWK to format txt files however ive run out of talent and could do with some help. Im not sure if this is possible using awk but I have an input as follows

Code:
L73-10 342 0 1480
L73-10 342 100 1480
L73-10 342 250 1656
L73-10 342 500 1746
L73-10 342 750 1910
L73-10 350 0 1480
L73-10 350 100 1480
L73-10 350 250 1656
L73-11 300 0 1480
L73-11 300 100 1480
L73-11 300 250 1656
L73-11 300 500 1746

$1 is line, $2 is cdp, $3 is time, $4 is velocity

I need the script to print something like
Code:
L73-10/342:0-1480,100-1480,250-1656,500-1746,750-1910/350:0-1480,100-1480,250-1656/
 
L73-11/300:0-1480,100-1480,250-1656,500-1746/

The number of Time Velocity values is never constant between lines and cdps so im unsure if using something like below is the right way to go.
Code:
{NF==4
if (NR==1)
printf $1"/"$2":"$3"-"$4"," 
$2=cdp
{
if (NR>1||$2==cdp)
printf ORS=$3"-"$4"," 
else
$2!=cdp(NR=1)
next
{
if (NR==1)
$2=cdp
printf $1"/"$2":"$3"-"$4"," 
{
if (NR>1||$2==cdp)
printf ORS=$3"-"$4"," 
}
}
}
}

This gives an error saying cdp is not defined however I dont understand how. Do I need to create a loop to repeat the process to the end of the file or is an array more appropriate?

The above script also does not account for "line" variations. I have written something to print all line values to separate files so if needs be I can run a script on each file. Also it does not separate the boundary between new cdp's with a /.

Hopefully Ive got the point across ok. This level of problem is well beyond my ability and I have little idea of whether im using the correct approach of trying to reset NR when cdp changes. Any help would be greatly appreciated.

Thanks
Ryan

Use CODE tags when posting code, data or logs to enhance readability and to preserve formatting like indention etc., ty.

Last edited by zaxxon; 08-04-2009 at 07:18 AM.. Reason: CODE tags
# 2  
Old 08-04-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums


+++++++++++++++++++++++++++++++++++++++++++++++++++++

Try this:

Code:
awk '
s==$1{printf(",%s-%s", $3, $4);next}
NR>1{print "\\"}
{s=$1;printf("%s/%s:%s-%s", $1, $2, $3, $4)}
END{print "\\"}' file

Regards
# 3  
Old 08-04-2009
Apologies Franklin52.

Your suggestion does not account for the change in cdp number. I think the output is the same as if I used
Code:
{NF==4
if (NR==1)
printf $1"/"$2":"$3"-"$4"," 
if (NR>1)
printf ORS=$3"-"$4"," 
}

Thanks
Ryan
# 4  
Old 08-04-2009
Sorry, I've misread the question, try this one:

Code:
awk '
t != $2 && s==$1{t=$2;printf("/%s:%s-%s", $2, $3, $4);next}
s==$1{printf(",%s-%s", $3, $4);next}
NR>1{print "/"}
{s=$1;t=$2;printf("%s/%s:%s-%s", $1, $2, $3, $4)}
END{print "/"}' file

This is my output:

Code:
$ cat file
L73-10 342 0 1480
L73-10 342 100 1480
L73-10 342 250 1656
L73-10 342 500 1746
L73-10 342 750 1910
L73-10 350 0 1480
L73-10 350 100 1480
L73-10 350 250 1656
L73-11 300 0 1480
L73-11 300 100 1480
L73-11 300 250 1656
L73-11 300 500 1746
$ awk '
t != $2 && s==$1{t=$2;printf("/%s:%s-%s", $2, $3, $4);next}
s==$1{printf(",%s-%s", $3, $4);next}
NR>1{print "/"}
{s=$1;t=$2;printf("%s/%s:%s-%s", $1, $2, $3, $4)}
END{print "/"}' file
L73-10/342:0-1480,100-1480,250-1656,500-1746,750-1910/350:0-1480,100-1480,250-1656/
L73-11/300:0-1480,100-1480,250-1656,500-1746/
$

Regards
# 5  
Old 08-04-2009
Franklin52,
Thanks very much. Ive spend so long trying to sort this.
Ryan
# 6  
Old 08-04-2009
Using arrays in awk :

Code:
awk ' BEGIN { s=0;t=0 } NR==FNR { if($2==s&&$1==t) { a[$1]=a[$1]","$3"-"$4;t=$1;s=$2 } else { a[$1]=a[$1]"/"$2":"$3"-"$4;t=$1
;s=$2 } }
END { for ( i in a ) { print i a[i]"/"} }' file_name.txt

# 7  
Old 08-04-2009
Thanks Panyam,
Ive tried to understand both suggestions but could anybody briefly explain what the code is saying?
Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk, sed, shell all words in INPUT.txt find in column1 of TABLE.txt and replce with column2 in

Hi dears i have text file like this: INPUT.txt 001_1_173 j nuh ]az 001_1_174 j ]esma. nuh ]/.xori . . . and have another text like this TABLE.txt j j nuh word1... (6 Replies)
Discussion started by: alii
6 Replies

2. Shell Programming and Scripting

Desired output.txt for reading txt file using awk?

Dear all, I have a huge txt file (DATA.txt) with the following content . From this txt file, I want the following output using some shell script. Any help is greatly appreciated. Greetings, emily DATA.txt (snippet of the huge text file) 407202849... (2 Replies)
Discussion started by: emily
2 Replies

3. UNIX for Dummies Questions & Answers

Removing PATTERN from txt without removing lines and general text formatting

Hi Everybody! First post! Totally noobie. I'm using the terminal to read a poorly formatted book. The text file contains, in the middle of paragraphs, hyphenation to split words that are supposed to be on multiple pages. It looks ve -- ry much like this. I was hoping to use grep -v " -- "... (5 Replies)
Discussion started by: AxeHandle
5 Replies

4. UNIX for Dummies Questions & Answers

awk formatting

Hi all, I'm writing a simple awk code: awk 'BEGIN {FS="|"};{print "Type\tNumber\ttypes\tTotal";};{print $1, "\t", $2, "\t", $3, "\t", $4, "\t";}' db_query.txt it gives me the result: Type Number types Total XXX 498.0 5100.0 5274.661 Type Number types Total... (7 Replies)
Discussion started by: messi777
7 Replies

5. Shell Programming and Scripting

awk append fileA.txt to growing file B.txt

This is appending a column. My question is fairly simple. I have a program generating data in a form like so: 1 20 2 22 3 23 4 12 5 43 For ever iteration I'm generating this data. I have the basic idea with cut -f 2 fileA.txt | paste -d >> FileB.txt ???? I want FileB.txt to grow, and... (4 Replies)
Discussion started by: theawknewbie
4 Replies

6. Shell Programming and Scripting

formatting awk

when i try this awk its giving out put as below. awk '!(/^$/||/--/||/selected/||/^ *$/){print $1}' tmp.txt output ===== 1 2010-08-03-12.31.26.126000 how excluede the 1st line ? i mean i want output only 2nd line i.e 2010-08-03-12.31.26.126000; (5 Replies)
Discussion started by: rocking77
5 Replies

7. Shell Programming and Scripting

AWK formatting help.

Dear all I require help with AWK regarding this situation Input is : fn1 12345 fn1 23456 fn3 231513 fn1 22325 fn3 123125 Desired output is fn1 12345 23456 22325 fn3 231513 123125 (5 Replies)
Discussion started by: Peasant
5 Replies

8. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies

9. UNIX for Dummies Questions & Answers

echo "ABC" > file1.txt file2.txt file3.txt

Hi Guru's, I need to create 3 files with the contents "ABC" using single command. Iam using: echo "ABC" > file1.txt file2.txt file3.txt the above command is not working. pls help me... With Regards / Ganapati (4 Replies)
Discussion started by: ganapati
4 Replies

10. Shell Programming and Scripting

Formatting using awk

Let's say I write a simple script that contains the following: date | awk '{print $1}' date | awk '{print $2}' Of course, when I run the script the output will look similar to: Tue Mar What if I want my ouput to be on one line as follows: Tue Mar What changes would I need to... (2 Replies)
Discussion started by: cdunavent
2 Replies
Login or Register to Ask a Question