Help required on AWK command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help required on AWK command
# 8  
Old 01-22-2008
Your command is working fine for the type of file you gave as an example. But consider the file I have given as an example in such a case the command

count=1
AWK -F"|" '$1 ~ /'$count'/ {printf $4}' file1

will give the output for all the numbers starting with 1 (eg 1, 100, 101..etc)in the first column of the table! Since ~ gives an approimate matching this won't help. I need an exact watch which is possible with ==
# 9  
Old 01-22-2008
Hello,

If you want to get the line starts with 100 means, you have to give like this :
count=100
awk -F"|" '$1 ~ /'$count'/ {print $3} file1

you can also give the search string like this :
$ search_string="aaa"
$ awk -F"|" '$2 ~ /'$search_string'/ {print $3} file1

count is not the line number. It's a search string only.
Hope you got better idea.
# 10  
Old 01-22-2008
I have understood your point of view btu I guess I couldn't properly express you with my problem. So, I am writing a part of my program underneath

my file is something like this with tabs and whitespaces inbetween and delimited by "|'

CAT file1
abc |123 |234
bcd |345 |456
cde |567 |678

Now my program is something like this:

start_cnt=1
end_cnt=200
while [ start_cnt -le end_cnt ]
do
var1=`AWK -F"|" '...........{ printf $2 }' file1`
echo "$var1" >> file2
start_cnt=`expr $start_cnt + 1`
done
echo "Finished file appending"

Now I don't know what to write in the space provided in the AWK command such that it will use the start_cnt as the incrementing variable starting with line 1 as start_cnt=1 and line 200 as start_cnt=200 and on each step will append the value at position $2 to file2

I hope now I could make you understand my problem!
# 11  
Old 01-22-2008
I think no need to use the loop in here.
You can just use the awk command and write the output to a file like this :
search_string="xxx"
awk -F"|" '$1 ~ '/$search_string/' { printf $2 }' file1 > file2
# 12  
Old 01-22-2008
Yes, that can be done but for a long table if you have to search each element of the field, you will require a lot of code.
Whereas I am looking for a very genaralised pattern!
# 13  
Old 01-22-2008
It's a bit expensive to scan the ENTIRE input file on EVERY iteration of the loop, but...
Given udi.txt:
Code:
abc |123 |234
bcd |345 |456
cde |567 |678

Code:
#!/bin/sh

start_cnt=1
end_cnt=200
while [ "${start_cnt}" -le "${end_cnt}" ]
do
   var1=`awk -F"|" -v fnr="${start_cnt}" 'FNR == fnr { printf $2 }' udi.txt`
   echo "$var1"
   start_cnt=`expr $start_cnt + 1`
done
echo "Finished file appending"

# 14  
Old 01-22-2008
Thanks a lot. It worked! I tried that way but I was using NR in place FNR.
Thanks again!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Required Command in awk command

Hi Firends, I wanted to extract the first record of the file which starst with character say "X". And I tried an awk command which works when i try to execute it individually: awk 'substr($1,1,1)=="X"' inputfile.txt But when I use the same command in my script for which I am passing the... (2 Replies)
Discussion started by: Ajay Venkatesan
2 Replies

2. Shell Programming and Scripting

awk help required

Hello, I am trying to get an output from awk and have been unsuccessful so far. There is an input file which contains certain segments, that needs to be read and append with another data after matching the segment... Input file is as shown below ISA*00* *00* *01*781495650... (2 Replies)
Discussion started by: rakeshv
2 Replies

3. Shell Programming and Scripting

awk script required

File 1 ################### TRAIU DSKDL; SLLA ;LAKJA KJAJA NSAJAN JANAL AJKJA JAJALA KAKAK JA AKA AKA AJ A A PPIN TRY1 SANT1 PPIN TRY2 SANT2 PPIN TRY3 SANT3 PPIN TRY4 SANT4 PPIN TRY5 SANT5 AJJA NA ANA ANHDJLD ALJALJA AJLJAJD LALJAL ALJALJA ALJALJA (4 Replies)
Discussion started by: jaita
4 Replies

4. Shell Programming and Scripting

Awk help required

Hi All, I have 2 .csv files as below File1.csv name1,20,30,date1 name2,30,40,date1 File2.csv name1,25,35,date1 name2,45,55,date1 Can any one help me to add the 2nd and 3rd column of file1 and file2 and write the output into a new file as below name1,45,65,date1... (9 Replies)
Discussion started by: ajay547
9 Replies

5. Shell Programming and Scripting

Searching using awk - Help required

Hi... I am working on script to search some records in a file based on certain fields and each record is a ASCII fixed size. I was using awk to search based on certain condition. But the length of the record is too much that awk is giving syntax error near unexpected token `(' Request... (5 Replies)
Discussion started by: ysrikanth
5 Replies

6. Shell Programming and Scripting

Help required with awk/sed

Hi I have a file, with format like: column1|coulumn2|column3|column4 A|X|K|18 L|O|R|31,42,25 G|H|I|55,66 L|E|Q|25,31,94 output required: column1|coulumn2|column3|column4 A|X|K|18 L|O|R|31,25 L|E|Q|25,31 Input File Format: All columns are seperated using |, last column... (8 Replies)
Discussion started by: New to awk
8 Replies

7. Cybersecurity

Help Required: Command to find IP address and command executed of a user

Hi, I am trying to write a script which would figure out who has run which command and their IP. As i dont have any clue as to which commands would do this job, i request some gurus to help me on this. Thanks Vishwas (2 Replies)
Discussion started by: loggedout
2 Replies

8. Shell Programming and Scripting

AWK script help required

Hello every one i have a very long file 'file1' like this <K>1</K> </Condition> <Tariff>Rate <Price>1.27</Price> <Interval>30</Interval> </Tariff> </Node> <Node>NonFaF <Tariff>Rate <Price>1.9</Price> <Interval>30</Interval> </Tariff> </Node> </Node> </Node> <Node>FaF (9 Replies)
Discussion started by: Dastard
9 Replies

9. Shell Programming and Scripting

Help Required regarding wc command

Hi guys, I want to find the number of records in a particular file and store that value in any other variable. I am trying this below command but it is not working and giving me an error "Uninary Operator Expected". say I have taken a variable name 'count' in which I have to store the no. of... (7 Replies)
Discussion started by: dtidke
7 Replies

10. Shell Programming and Scripting

Help required on awk

Hi, I have a fixed with file, which have almost 10 records Sample File: ------------ AR 100 TTT BC 200 SSS DA 199 YYY AR 500 RRR PO 300 QQQ PB 800 PPP PC 150 OOO AR 111 CCC AD 321 LLL I Have to check for first two charactors and if its 'AR' i have to save those records to one... (3 Replies)
Discussion started by: meetavin
3 Replies
Login or Register to Ask a Question