Need advise to process the file with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need advise to process the file with awk
# 1  
Old 07-12-2010
Need advise to process the file with awk

Hi gurus,

i need your advise on how to process this file using awk.
i have this file
Code:
                        COLA  COLB  COLC  COLD  COLE  COLF  COLG  COLH
 AAAA                   86    111   122   133   144   155   266   377
 BBBB                   70    211   222   233   244   255   266   377

but i want to take some of the columns only, but keep the formatting, so the desired output is like

Code:
                        COLA  COLB  COLC  COLD  COLG  COLH
 AAAA                   86    111   122   133   266   377
 BBBB                   70    211   222   233   266   377

i tried using awk, but i cannot get the formatting right,
and COLA is considered as column 1 instead of column 2

Many thanks in advance for your help.
# 2  
Old 07-12-2010
Maybe ...:

Code:
#!/bin/bash

LC=0
while read C1 C2 C3 C4 C5 C6 C7 C8 C9
do
  LC=$(($LC+1))
  if [ $LC -eq 1 ]
  then
    printf '\t %s \t %s \t %s \t %s \t %s \t %s \n' $C1 $C2 $C3 $C4 $C7 $C8
  else
    printf '%s \t %s \t %s \t %s \t %s \t %s \t %s \n' $C1 $C2 $C3 $C4 $C5 $C8 $C9
  fi
done < in.file

exit 0
#finis

Code:
[house@leonov] bash code.bash
         COLA    COLB    COLC    COLD    COLG    COLH
AAAA     86      111     122     133     266     377
BBBB     70      211     222     233     266     377

# 3  
Old 07-12-2010
hi dr.house

thanks, the formatting looks great, but when i tried this is my output

Code:
#!/bin/bash

LC=1
while read C1 C2 C3 C4 C5 C6 C7 C8 C9
do
  if [ $LC -eq 1 ]
  then
    printf '\t %s \t %s \t %s \t %s \t %s \t %s' $C1 $C2 $C3 $C4 $C7 $C8
  else
    printf '%s \t %s \t %s \t %s \t %s \t %s \t %s' $C1 $C2 $C3 $C4 $C5 $C8 $C9
  fi
  printf '\n'  LC=$(($LC+1))
done < in.file

#finis

Code:
[root@localhost ~]# ./bash 
         COLA    COLB    COLC    COLD    COLG    COLH
         AAAA    86      111     122     155     266
         BBBB    70      211     222     255     266

looks like line 2 and line 3 is read as LC=1
# 4  
Old 07-12-2010
I updated my code while you were testing it (sorry ...). Therefore, please re-run your test with the (hopefully) debugged version currently published in my recent post.
# 5  
Old 07-12-2010
hi dr. house

thanks for your help, it works perfectly,
will try to play around with it.
# 6  
Old 07-12-2010
Code:
$ cat infile
                       COLA  COLB  COLC  COLD  COLE  COLF  COLG  COLH
 AAAA                   86    111   122   133   144   155   266   377
 BBBB                   70    211   222   233   244   255   266   377

$ head -1 infile|awk '{printf "%15s %s %s %s %s %s\n", $1, $2, $3, $4, $7, $8}' > outfile

$ awk 'NR > 1 {printf "%-10s %-4s %-4s %-4s %-4s %-4s %-4s\n", $1, $2, $3, $4, $5, $8, $9}' infile >> outfile

$ cat outfile
           COLA COLB COLC COLD COLG COLH
AAAA       86   111  122  133  266  377 
BBBB       70   211  222  233  266  377

# 7  
Old 07-12-2010
Hi Soleil
Thanks for another great solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Process a file for line count using for loop in awk

Hi, I have a file with contents So what I have to do is In short, break the file after every 6 lines and then truncate new line to tab for these 6 lines. I am not able to execute the for loop in awk properly. The idea is something like this: less file| awk '{for .... {if ((nr>=1)... (7 Replies)
Discussion started by: rossi
7 Replies

2. UNIX for Advanced & Expert Users

Using awk to read a file and process

I am fairly green using awk so I don't have anything started but what I am trying to do is: I have a file called "contacts" and in the file are 3 fields separate by ;. I want to read each line and send an email script. I have the email function working but just need to know how to setup awk to... (8 Replies)
Discussion started by: ziggy6
8 Replies

3. Shell Programming and Scripting

awk & basename puzzler - advise sought

Hi I have been able generate a file ($ELOG) that can have multiple lines within it. The first column represents the full path source file and the other is the full path target ... the file names are the same but the target directory paths are slightly different. <source_dir1>/file1 ... (4 Replies)
Discussion started by: davidra
4 Replies

4. Shell Programming and Scripting

Solaris script using awk giving errors - please advise

I'm using solaris 10 Scenario as follows I have a logfile with 2 columns: column 1 = source directory + filename column 2 = destination directory + filename Using cron, my script polls for new files and adds them to the logfile ($ELOG) as described above. Using sed, the distination... (2 Replies)
Discussion started by: davidra
2 Replies

5. Shell Programming and Scripting

How to make AWK process an input file many many times?

By "many many times" I mean the times the input file is to be processed is unknown beforehand, it will be known when awk finishes processing the input file for the first time. So my question is: how to start over again from the first record of the input file when AWK finishes processing the... (7 Replies)
Discussion started by: kevintse
7 Replies

6. Shell Programming and Scripting

awk: process file

Hello, i have a file as below: ... AAA: 1 BBB: 2 CCC: 3 AAA: 11 BBB: 22 CCC: 33 AAA: 111 BBB: 222 CCC: 333 .... how to process it by using AWK to this: (AAA BBB CCC) .... 1 11 111 2 22 222 3 33 333 (2 Replies)
Discussion started by: 0916981
2 Replies

7. Shell Programming and Scripting

Need help with a script to process a CSV file using SED and AWK

I get a CSV file every day with 2 columns and multiple rows ex: date1,date2 ( both the fields are varchar fields) This data has to be updated in a table which is being done manually and i want to automate that. 1. I have to select all the data from the prod table( 2 columns { date1,date2}) into... (4 Replies)
Discussion started by: kkb
4 Replies

8. 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

9. Shell Programming and Scripting

process text file with awk

I have a text file which represent a http packet: header1 haeder2 ..... ..... headern payload I need to count bytes in the payload. How can I get it using awk? Thanks in advance Andrea Musella (2 Replies)
Discussion started by: littleboyblu
2 Replies
Login or Register to Ask a Question