Sponsored Content
Top Forums Shell Programming and Scripting AWK Merge Fields for Print Output Post 302141649 by bakunin on Sunday 21st of October 2007 09:26:40 AM
Old 10-21-2007
Quote:
Originally Posted by RacerX
Where did i go wrong?
You took what was meant as a code fragment for demonstration purposes and put it to work without trying to understand it. First off, you have the variables "field3" and "field4" nowhere assigned, so chances are they don't hold what they are supposed to hold.

Second, i just found a typo in the code, which you also haven't noticed:

output=sprintf("%s %s / %s:", output, field1, field2)

should of course be

output=sprintf("%s %s / %s:", output, field2, field3)

but as it was just to show you the mechanism you could write that completely different anyways. I didn't care if the 10 blanks i put in if the two fields were empty are really correct, maybe it's more or less. The whole purpose of the if()-statement is: "if the one field AND the other field is empty, we put blanks at the end of the output string, otherwise the content of the first field, then a slash, then the second field". THIS was, what the code tried to show you. The blanks are just there to maintain the column format of the output. Possible output should like:

Code:
field1="A" or empty
field2="B" or empty

... A/B ...  // fields non-empty - print them with a "/" in between
...    ...  // fields empty - print an equal amount of spaces

So, go over your code again, remove any output which is just "passed through" (all the fields which are just read and written without any modification) and analyze the output of the stripped-down program. Find out, where the program produces the desired results and where it doesn't. Change it accordingly. Only when you have solved the tricky parts put in the other fields again, one by one. Let the program run in the different stages of development to see if it still works the way you want it to work.

By making little steps instead of giant leaps between runs you can always isolate occurring problems as they are introduced. If you write 10 lines and they do what the should, then add another 5 lines and the program doesn't do what it should any more you will *know* already that the problem is within the 5 lines last written.

There is an old roman proverb, "divide et impera" (set apart and rule). The same is true for programming. Nobody is able to analyze, write or conceive huge amounts of code at once, make them into small portions and study one after the other.

It is like solving the problem "build a house": don't try to build a house, try to build a wall first, as a house hast four walls and a roof. Don't even try to build a wall, try to reduce building the wall to solving the problem "lay one brick onto the other" - now, *this* is a manageable problem, which is easily solved, so "build a wall" is a long succession of "lay one brick onto the other"-problems. From there, develop the next step, say "how to combine 2 walls to form an edge", etc., etc., until you have a "build a house"-program, which is now a large array of little solveable problems, which are solved separately.

bakunin
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: How to merge duplicate lines and print in a single

The input file: >cat module1 200611051053 95 200523457498 35 200617890187 57 200726098123 66 200645676712 71 200744556590 68 >cat module2 200645676712 ... (10 Replies)
Discussion started by: winter9
10 Replies

2. Shell Programming and Scripting

awk /nawk :: print the everything except the first and the last fields

format of file1 "file1.txt" 1 2 3 4 A B C XX YY ZZ AA WWW The output must contain except the first and last column the output must be 2 3 B YY ZZ AA (8 Replies)
Discussion started by: centurion_13
8 Replies

3. Shell Programming and Scripting

AWK: merge two files and replace some fields

Need some code tweak: awk 'END { for (i=1; i<=n; i++) if (f2]) print f2] } NR == FNR { f2 = $1] = $0 next } $1 in f2 { delete f2 }1' FS=, OFS=, 2.csv 1.csv > 3.csvfile 1.csv have: $1,$2,$3,$4,$5,$6,$7,$8,$9...... file 2.csv have: $1,$2,$3,$4,$5,$6 (2 Replies)
Discussion started by: u10
2 Replies

4. Shell Programming and Scripting

Print all the fields of record using awk

Hi, i want to generate print statement using awk. i have 20+ and 30+ fields in each line Now its priting only first eight fields print statement as output not all. my record is as shown below filename ... (2 Replies)
Discussion started by: raghavendra.nsn
2 Replies

5. Shell Programming and Scripting

awk - print all fields except for last field

How do I print all the fields of a record except for the $(NF) field? (4 Replies)
Discussion started by: locoroco
4 Replies

6. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

7. Shell Programming and Scripting

How to merge fields in a single file using awk ?

Hi, From a file, using: awk -F" " '{ if (NF == 6) print $1, $2, $3, $4, $5, $6; if (NF == 5) print $1, $2, $3, $4, $5; }' i printed out the required output. But i'm trying to merge the columns. Please look at the desired output. Any suggestions? Thanks Output: 00015 PSA1 ... (5 Replies)
Discussion started by: web2moha
5 Replies

8. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

9. UNIX for Dummies Questions & Answers

Merge two text files by two fields and mixed output

Hello, I'm back again looking for your precious help- This time I need to merge two text files with matching two fields, output only common records with mixed output. Let's look at the example: FILE1 56153;AAA0708;3;TEST1TEST1; 89014;BBB0708;3;TEST2TEST2; 89014;BBB0708;4;TEST3TEST3; ... (7 Replies)
Discussion started by: emare
7 Replies

10. Shell Programming and Scripting

awk print even fields of file

Hello: I want to print out the even number of fields plus the first column as row identifiers. input.txt ID X1 ID X2 ID X3 ID X4 A 700 A 1200 A 400 A 1300 B 2000 B 1000 B 2000 B 600 C 1400 C 200 C 1000 C 1200 D 1300 D 500 D 600 D 200and the output is: output.txt ID X1 X2 X3... (3 Replies)
Discussion started by: yifangt
3 Replies
DBF_DUMP(1p)						User Contributed Perl Documentation					      DBF_DUMP(1p)

NAME
dbf_dump - Dump the record of the dbf file FORMAT
dbf_dump [options] files where options are --rs output record separator (default newline) --fs output field separator (default colon) --fields comma separated list of fields to print (default all) --undef string to print for NULL values (default empty) --memofile specifies unstandard name of attached memo file --memosep separator for dBase III dbt's (default x1ax1a) --nomemo do not try to read the memo (dbt/fpt) file --info print info about the file and fields with additional --SQL parameter, outputs the SQL create table --version print version of the XBase library --table output in nice table format (only available when Data::ShowTable is installed, overrides rs and fs) SYNOPSIS
dbf_dump -fields id,msg table.dbf dbf_dump -fs=' : ' table dbf_dump --nomemo file.dbf ssh user@host 'cat file.dbf.gz' | gunzip - | dbf_dump - DESCRIPTION
Dbf_dump prints to standard output the content of dbf files listed. By default, it prints all fields, separated by colons, one record on a line. The output record and column separators can be changed by switches on the command line. You can also ask only for some fields to be printed. The content of associated memo files (dbf, fpt) is printed for memo fields, unless you use the "--nomemo" option. You can specify reading the standard input by putting dash (-) instead of file name. AVAILABLE FROM
http://www.adelton.com/perl/DBD-XBase/ AUTHOR
(c) 1998--2011 Jan Pazdziora. SEE ALSO
perl(1); XBase(3pm); index_dump(1p) perl v5.12.4 2011-08-31 DBF_DUMP(1p)
All times are GMT -4. The time now is 01:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy