row seperate


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting row seperate
# 1  
Old 07-28-2003
row seperate

i have this sample data:

test01 --- abc-01 name1
abc-02 name2
abc-03 name3

test02 --- abc-20 name4
abc-21 name5

test03 --- abc-22 name6
abc-23 name7


i want to generate a file that looks like:

test01 abc-01 name1
test01 abc-02 name2
test01 abc-03 name3
test02 abc-20 name4
test02 abc-21 name5
test03 abc-22 name6
test03 abc-23 name7

i know awk can use RS (record seperator) but i dont know much of it. pls help Smilie

thanks!
# 2  
Old 07-28-2003
Here's your sample data in a code block to remove ambiguity about spacing after fishing around in "view page source" in my browser. I also realigned it to what I guess your data really looks like. The fact of spacing matters more to awk than the amount of spacing anyhow, but everyone who posts here should be aware that alignment gets ruined by the VBcode->HTML conversion unless it's in a code block.
Code:
test01 ---   abc-01     name1
             abc-02     name2
             abc-03     name3

test02 ---   abc-20     name4
             abc-21     name5

test03 ---   abc-22     name6
             abc-23     name7

Fiddling with RS is not helpful here.
This should do what you want, though it is untested code, and you shouldn't run it until you understand it.
Code:
#!/usr/bin/awk
NF == 0 { next }                      # Skip empty lines
NF == 4 { tag = $1 }                  # Pick up the tag
        { print tag, $(NF - 1), $NF } # Print lines with data

The moderators will probably want to move this to the shell scripting forum.Smilie
# 3  
Old 07-28-2003
You know more than I do about awk - I would've come up with some convoluted mess to do that. Smilie

I can tell what your code is doing, but I'm wondering, won't the two lines
Code:
NF == 4 { tag = $1 }                  # Pick up the tag
        { print tag, $(NF - 1), $NF } # Print lines with data

only pick up on, say,
Code:
test01 ---   abc-01     name1

but not
Code:
             abc-02     name2
             abc-03     name3

?
# 4  
Old 07-29-2003
Data sorry about the wrong forum

hey, you are right about the spacing and the forum. i think this topic should be moved with the shell scripting forum. will the moderators do this for me :P

thanks alot!
# 5  
Old 07-29-2003
Quote:
Originally posted by oombera
[W]on't the two lines
Code:
NF == 4 { tag = $1 }                  # Pick up the tag
        { print tag, $(NF - 1), $NF } # Print lines with data

only pick up on, say,
Code:
test01 ---   abc-01     name1

but not
Code:
             abc-02     name2
             abc-03     name3

?
Nope. Your confusion may come from a mistaken notion that the pattern/action pairs look like exclusive cases, but you should look at them like a C switch statement: You fall through to the next "case" (awk "pattern") unless you encounter a "break" (awk "next"). So NF == 0 { next } skips empty lines. Then NF == 4 { tag = $1 } picks up the first field and goes to the next pattern when there are four fields. The next pattern is empty, so it matches every line that wasn't "next"-ed so far, and its action is to print the saved tag and the last two fields from the current input line. If there is exactly one field on a line, awk will break in this last case, but that wasn't part of the OP's stated problem.
# 6  
Old 07-29-2003
how to code block

how do i code block? i would like to make some corrections in my post :P
# 7  
Old 07-30-2003
When you're making a post to the forums, you should see some buttons above the textbox you type your message in. Look for a button with the # symbol on it to insert code. Read the help page for more on VB Code.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

This is a question that is related to one I had last August when I was trying to sort/merge two files by millsecond time column (in this case column 6). The script (below) that helped me last august by RudiC solved the puzzle of sorting/merging two files by time, except it gets lost when the... (0 Replies)
Discussion started by: aachave1
0 Replies

2. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

3. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

My original files are like this below and I distinguish them from the AP_ID (file1 has 572 and file2 has 544). Also, the header on file1 has “G_” pre-pended. NOTE: these are only snippets of very large files and much of the data is not present here. Original File 1: ... (36 Replies)
Discussion started by: aachave1
36 Replies

4. Shell Programming and Scripting

Add Row from First Row (Split Row)

HI Guys, I have Below Input :- RepigA_hteis522 ReptCfiEtrBsCll_aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 RepigA ReptCfiEtrBsCll hteis522 aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 Split Data in two first row... (2 Replies)
Discussion started by: pareshkp
2 Replies

5. Shell Programming and Scripting

Get row number from file1 and print that row of file2

Hi. How can we print those rows of file2 which are mentioned in file1. first character of file1 is a row number.. for eg file1 1:abc 3:ghi 6:pqr file2 a abc b def c ghi d jkl e mno f pqr ... (6 Replies)
Discussion started by: Abhiraj Singh
6 Replies

6. Shell Programming and Scripting

In php, Moving a new row to another table and deleting old row

Hi, I already succeed moving a new row to another table if the field from new row doesn't have the first word that I categorized (like: IRC blablabla, PTM blablabla, ADM blablabla, BS blablabla). But it can't delete the old row. Please help me with the script. my php script: INSERT INTO... (2 Replies)
Discussion started by: jazzyzha
2 Replies

7. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

8. UNIX for Dummies Questions & Answers

Shell Script: Traverse Database Table Row by Row

Hello Everyone, My issue is that I want to traverse a database table row by row and do some action on the value retrieved in each row. I have gone through a lot of shell script questions/posts. I could find row by row traversal of a file but not a database table. Please help. Thanks &... (5 Replies)
Discussion started by: ahsan.asghar
5 Replies

9. Shell Programming and Scripting

awk command : row by row merging of two files

I want to write a scrpit to merge files row wise (actually concatinating) main.txt X Y Z file 1 A B C file 2 1 2 3 now i want the script to check if the file1 is empty or not, if empty then make it like A B C 1 2 3 again to check if second file is empty if not do as done... (0 Replies)
Discussion started by: shashi792
0 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question