Help Using awk to Extract Data


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help Using awk to Extract Data
# 1  
Old 04-21-2011
Help Using awk to Extract Data

Hi. Im new to UNIX also in programming language which in need help to output like what was I indicated using either awk shell programming or combination of some commands. Correct me if im in the wrong section. Thanks in advance.

Input
Code:
101 The quick brown fox jumps over the lazy dog
 99 The quick brown fox jumps over the lazy dog
  1 The quick brown fox jumps over the lazy dog
 29 The quick brown fox jumps over the lazy dog
 77 The quick brown fox jumps over the lazy dog
122 The quick brown fox jumps over the lazy dog
 36 The quick brown fox jumps over the lazy dog
  7 The quick brown fox jumps over the lazy dog
 29 The quick brown fox jumps over the lazy dog
 57 The quick brown fox jumps over the lazy dog
and so on..

Output
Code:
101 The fox jumps the dog
 99 The fox jumps the dog
  1 The fox jumps the dog
 29 The fox jumps the dog
 77 The fox jumps the dog
122 The fox jumps the dog
 36 The fox jumps the dog
 7  The fox jumps the dog
 29 The fox jumps the dog
 57 The fox jumps the dog
and so on...

# 2  
Old 04-21-2011
Try:
Code:
awk '{print $1,$2,$5,$6,$8,$10}' input

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 04-21-2011
Quote:
Originally Posted by bartus11
Try:
Code:
awk '{print $1,$2,$5,$6,$8,$10}' input

Sir Thanks for the code, i already try this one but got the same result.
Result
Code:
101 The fox jumps the dog
99 The fox jumps the dog
1 The fox jumps the dog
29 The fox jumps the dog
77 The fox jumps the dog
122 The fox jumps the dog
36 The fox jumps the dog
7 The fox jumps the dog
29 The fox jumps the dog
57 The fox jumps the dog

Is there a way to separate them or align them like this one?
Code:
101 The fox jumps the dog
 99 The fox jumps the dog
  1 The fox jumps the dog
 29 The fox jumps the dog
 77 The fox jumps the dog
122 The fox jumps the dog
 36 The fox jumps the dog
  7 The fox jumps the dog
 29 The fox jumps the dog
 57 The fox jumps the dog

And also this is only a sample file, the real data of my file are lines containing time,dates progress etc which are align in column all time are align each other in time column, all dates are align also, progress align each other, etc. Thanks
# 4  
Old 04-21-2011
For this data you can use:
Code:
awk '{printf "%3s ",$1;print $2,$5,$6,$8,$10}' input

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 04-21-2011
A lil' bit more generic formatting approach - a bit verbose, but.....
Code:
nawk '
   BEGIN {fN=split("1 2 5 6 8 10", fA, FS)}
   FNR==NR {
      for(i=1;i in fA;i++)
        max[i]=(max[i]<length($fA[i]))?length($fA[i]):max[i]
      next
  }
  {
      for(i=1;i in fA;i++)
        printf("%*s%c", max[i],$fA[i],(i==fN)?ORS:OFS)
  }' myFile myFile

# 6  
Old 04-21-2011
Quote:
Originally Posted by vgersh99
A lil' bit more generic formatting approach - a bit verbose, but.....
Code:
nawk '
   BEGIN {fN=split("1 2 5 6 8 10", fA, FS)}
   FNR==NR {
      for(i=1;i in fA;i++)
        max[i]=(max[i]<length($fA[i]))?length($fA[i]):max[i]
      next
  }
  {
      for(i=1;i in fA;i++)
        printf("%*s%c", max[i],$fA[i],(i==fN)?ORS:OFS)
  }' myFile myFile

*newbie question*
@vgersh99 - Thanks, But how can i run this script if im using Windows Services for UNIX only?

@bartus11 - Thanks, how can i separate two column or just add space to separate $1 to $2?


Thanks for your help.
# 7  
Old 04-21-2011
Quote:
Originally Posted by bankai29
*newbie question*
@vgersh99 - Thanks, But how can i run this script if im using Windows Services for UNIX only?
Thanks for your help.
The same way you'd run any other script/command: create a file, copy/paste the content of what's been provided, change the file permissions (chmod +x myFile), and run the script as './myFile'
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk used to extract data between text

Hello all, I have a file (filename.txt) with some data (in two columns X and Y) which looks like this: ########## 'Header1' 'Sub-header1' X Y xxxx.xx yyyy.yyy xxxx.xx yyyy.yyy .... ... 'Sub-header2' X Y xxxx.xx ... (7 Replies)
Discussion started by: jaldo0805
7 Replies

2. Shell Programming and Scripting

extract the data using AWK command

In a file i have a data like INPUT: no,name,company 1,vivek,hcl 2,senthil,cts 1,narsi,hcl 4,prabhakaran,ibm OUTPUT: 1,vivek,hcl 1,narsi,hcl Using AWK command i want to display the names those having no:1 and company:hcl.Please tell me the command to display above result. (8 Replies)
Discussion started by: katakamvivek
8 Replies

3. Shell Programming and Scripting

extract data with awk

i have a following output file PF Release 2.4 on SERVICE at Mon Feb 6 18:41:02 2012 ---------------------------------------- ---------------- |pPF |SEP |CAPS |CALLS |OPEN | |-------------------------------------------------------------| | 0 ---... (1 Reply)
Discussion started by: gauravah
1 Replies

4. Shell Programming and Scripting

Extract data with awk and write to several files

Hi! I have one file with data that looks like this: 1 data data data data 2 data data data data 3 data data data data . . . 1 data data data data 2 data data data data 3 data data data data . . . I would like to have awk to write each block to a separate file, like this: 1... (3 Replies)
Discussion started by: LinWin
3 Replies

5. Shell Programming and Scripting

extract data with awk from html files

Hello everyone, I'm new to this forum and i am new as a shell scripter. my problem is to have html files in a directory and I would like to extract from these some data that lies between two different lines Here's my situation <td align="default"> oxidizability (mg / l): data_to_extract... (6 Replies)
Discussion started by: sbobotex
6 Replies

6. UNIX for Dummies Questions & Answers

AWK, extract data from multiple files

Hi, I'm using AWK to try to extract data from multiple files (*.txt). The script should look for a flag that occurs at a specific position in each file and it should return the data to the right of that flag. I should end up with one line for each file, each containing 3 columns:... (8 Replies)
Discussion started by: Liverpaul09
8 Replies

7. Shell Programming and Scripting

Extract Data - awk

I need to extract columns but the way it should be stored in a file is different.I can simply do a cut -f3,2 filename but the problem is even if i do it so and the values in column 2 are string then col 2 would be appear before col3 I tried awk but using the substr i think its not possible to... (8 Replies)
Discussion started by: dinjo_jo
8 Replies

8. Shell Programming and Scripting

sed or awk to extract data from Xml file

Hi, I want to get data from Xml file by using sed or awk command. I want to get the following result : mon titre 1;Createur1;Dossier1 mon titre 1;Createur1;Dossier1 and save it in cvs file (fichier.cvs). FROM this Xml file (test.xml): <playlist version="1"> <trackList> <track>... (1 Reply)
Discussion started by: yeclota
1 Replies

9. Shell Programming and Scripting

extract data from xml- shell script using awk

Hi, This is the xml file that i have. - <front-servlet platform="WAS4.0" request-retriever="SiteMinder-aware" configuration-rescan-interval="60000"> <concurrency-throttle maximum-concurrency="50" redirect-page="/jsp/defaulterror.jsp" /> - <loggers> <instrumentation... (5 Replies)
Discussion started by: nishana
5 Replies

10. Shell Programming and Scripting

Extract data segment using awk??

How do I filter a long report, with the "STARTWORD" and "STOPWORD" as the variables to use in my awk command, to print the whole data segment that only contains the matched start/stop word? awk '/start/, /stop/' file <- this prints the line, though I need to print the whole segment. Newline... (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question