awk command to find seq lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command to find seq lines
# 1  
Old 01-07-2014
awk command to find seq lines

I have a below file

Code:
FILE.cfg
JAN_01 
VAR1=4
VAR2=SUM
VAR3=PRIVATE
 
JAN_10
VAR1=44
VAR2=GUN
VAR3=NATUR
 
JAN_20
VAR1=3
VAR2=TQN
VAR3=COMMA
 
code: (JAN_10 is argument passed from script)
Var1=find_cfg(JAN_10)
Var2=find_cfg(JAN_10)
Var3=find_cfg(JAN_10)
 
find_cfg
{
awk '/JAN_10/ {for(i=1; i<=4; i++) {getline; print}}' FILE.cfg
}

The above awk command get's the next 4 lines, but how to get the related details and save like below ??

Code:
Var1=44
Var2=GUN
Var3=NATUR

# 2  
Old 01-07-2014
You need the lines with 'VAR' alone in the output ?
if so just grep it.

What you mean by related details ?
What exactly you need in the output ?
# 3  
Old 01-07-2014
Argument passed to the script is JAN_10, using that search in the FILE.cfg file and getting next four lines from FILE.cfg,
Code:
say,
VAR1=44
VAR2=GUN
VAR3=NATUR

Code:
command line: script.sh JAN_10
 
script.sh:
#!/bin/ksh
function find_cfg
{
awk '/JAN_10/ {for(i=1; i<=3; i++) {getline; print}}' FILE.cfg
}
 
VAR1=find_cfg(JAN_10) #<---44
VAR2=find_cfg(JAN_10) #<---GUN
VAR3=find_cfg(JAN_10) #<---NATUR

Want to pass the values like highlighted according to argument passed to script
# 4  
Old 01-07-2014
One of the way would be to source the file. Try
Code:
michaelf>cat fil.ksh
#!/bin/ksh

#function
find_cfg()
{
awk "/$1/ {for(i=1; i<=4; i++) {getline; print}}" FILE.cfg>/home/michael/outfil
}

#call the function
find_cfg JAN_10

#source the file
. /home/michael/outfil

#check the values
echo $VAR1
echo $VAR2
echo $VAR3
michaelf>chmod u+x fil.ksh
michaelf>./fil.ksh
44
GUN
NATUR
michaelf>


Last edited by michaelrozar17; 01-07-2014 at 03:59 AM..
# 5  
Old 01-07-2014
If the number of variables associated with different strings could vary, you might want to try:
Code:
awk -v string="$1" '
copy && $1 == "" { exit }
copy
$1 == string { copy = 1 }
' file

If you're using a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
# 6  
Old 01-07-2014
You can do like this no need to write to some file and source

Code:
$ cat file.cfg 
JAN_01 
VAR1=4
VAR2=SUM
VAR3=PRIVATE
 
JAN_10
VAR1=44
VAR2=GUN
VAR3=NATUR
 
JAN_20
VAR1=3
VAR2=TQN
VAR3=COMMA

Code:
$ cat querry.sh
#!/bin/ksh

file="file.cfg"
t_lines="4"

find_cfg(){
                 awk -v start="$1" \
                     -v stop="$t_lines" '
                     $0 == start{i=1;next}i{++i;print}i==stop{exit}' $file 
          }

find_cfg $1 | \
while read line; do
        export $line && eval echo '$'${line%=*}    
done

Code:
$ ksh querry.sh JAN_10 
44
GUN
NATUR


Last edited by Akshay Hegde; 01-07-2014 at 07:41 AM.. Reason: Bold - -Color
# 7  
Old 01-07-2014
Quite some ifs:
If I understand correctly that you want to define shell variables AND
if your file has really empty lines (yours above doesn't) separating records AND
if you know exactly, what you're doing when using the dangerous eval,

try
Code:
eval $(awk '/^$/&&p {exit} p; /^JAN_10/ {p=1}' file
echo $VAR1
44

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

find pattern matches in consecutive lines in certain fields-awk

I have a text file with many thousands of lines, a small sample of which looks like this: InputFile:PS002,003 D -1 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 6 6 -1 -1 -1 -1 0 509 0 PS002,003 PSQ 0 1 7 18 1 0 -1 1 1 3 -1 -1 ... (5 Replies)
Discussion started by: jvoot
5 Replies

2. Shell Programming and Scripting

Help with awk script to append seq num at end of record

Hi Unix forum. I have the following requirement to add a sequence value to each record in a file but only if it meets certain conditions. Field value in pos. 1 and 2 must be '0B' or 'OA' else leave as is. Sequence value must be preserved for each OB and OA pair. Data Before: 123 456... (5 Replies)
Discussion started by: pchang
5 Replies

3. Shell Programming and Scripting

Using awk or sed to find a pattern that has lines before and after it

Dear gurus, Please help this beginner to write and understand the required script. I am looking for useing awk for sed. I have a few thousand lines file whose contain are mostly as below and I am trying to achieve followings. 1. Find a string, say user1. Then hash the line containing the... (6 Replies)
Discussion started by: ran_bon_78
6 Replies

4. Shell Programming and Scripting

[awk] find pattern, change next two lines

Hi, hope you can help me... It seems like a straightforward problem, but I haven't had any success so far using my basic scripting and awk "skills": I need to find a pattern /VEL/ in an input file that looks like this: 1110SOL OW25489 1.907 7.816 26.338 -0.4365 0.4100 -0.0736 ... (3 Replies)
Discussion started by: origamisven
3 Replies

5. Shell Programming and Scripting

Trying to find the distinct lines using uniq command

Platform :Oracle Linux 6.4 Shell : bash The below file has 7 lines , some of them are duplicates. There are only 3 distinct lines. But why is the uniq command still showing 7 ? I just want the distinct lines to be returned. $ cat test.txt SELECT FC.COORD_SET_ID FROM OM_ORDER_FLOW F, -... (2 Replies)
Discussion started by: kraljic
2 Replies

6. Shell Programming and Scripting

Find duplicates in column 1 and merge their lines (awk?)

Hi, I have a file (sorted by sort) with 8 tab delimited columns. The first column contains duplicated fields and I need to merge all these identical lines. My input file: comp100002 aaa bbb ccc ddd eee fff ggg comp100003 aba aba aba aba aba aba aba comp100003 fff fff fff fff fff fff fff... (5 Replies)
Discussion started by: falcox
5 Replies

7. Shell Programming and Scripting

awk to find pattern and add lines

My file goes like this: SID_LIST_HOSTNAME_LISTENER_3 = (SID_LIST = (SID_DESC = (SID_NAME = ORA0008) (ORACLE_HOME = /opt/oracle/product/ORA0008) (ENVS = "LD_LIBRARY_PATH=/opt/oracle/product/ORA0008/lib") ) (SID_DESC = (SID_NAME = ORA0007) ... (4 Replies)
Discussion started by: jpsingh
4 Replies

8. Shell Programming and Scripting

awk find a string, print the line 2 lines below it

I am parsing a nagios config, searching for a string, and then printing the line 2 lines later (the "members" string). Here's the data: define hostgroup{ hostgroup_name chat-dev alias chat-dev members thisisahostname } define hostgroup{ ... (1 Reply)
Discussion started by: mglenney
1 Replies

9. Shell Programming and Scripting

script to find filenames with latest version and for all seq. numbers in a day

Hi, We have a requirement to find the set of filenames from the group of files in a specified folder based on (i) version number (ii) sequence number such that, for any given sequence number in a day only the latest version filenames have to indentified. Below is the format of... (4 Replies)
Discussion started by: Deepakbabu
4 Replies

10. Shell Programming and Scripting

awk/sed Command : Parse parameter file / send the lines to the ksh export command

Sorry for the duplicate thread this one is similar to the one in https://www.unix.com/shell-programming-scripting/88132-awk-sed-script-read-values-parameter-files.html#post302255121 Since there were no responses on the parent thread since it got resolved partially i thought to open the new... (4 Replies)
Discussion started by: rajan_san
4 Replies
Login or Register to Ask a Question