Fetch field to variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fetch field to variable
# 1  
Old 12-09-2016
Fetch field to variable

Input.txt
Code:
NOTE1|VALUE1|HMM|UPSTR|
NOTE2||HMP|UPSTR|
NOTE3|VALUE3|HML|UPSTR|
NOTE4||HMD|UPSTR|
NOTE5|VALUE5|HMS|UPSTR|
NOTE6|VALUE6|HSD|UPSTR|
NOTE7||HSS|UPSTR|
NOTE8|VALUE8|HEE|UPSTR|
NOTE9|VALUE9|HER|DOWNSTR|
NOTE50|VALUE1|HEE|DOWNSTR|

I am trying to read field 1, 2 and 3 from the Input.txt file when field 4 matches pattern UPSTR

Code tried:
Code:
awk -F"|" '$4 ~ /UPSTR/{print $1" "$2" "$3}' Input.txt | while read VAR1 VAR2 VAR3
do
operation
done

But when I use the above code to read files it is not working properly when some fields are empty(one which are highlighted)
If filed two is empty, I want VAR2 to be empty

Code:
EX: Line2: 
NOTE2||HMP|UPSTR|
VAR1="NOTE2"
VAR2=""
VAR3="HMP"

# 2  
Old 12-09-2016
No awk needed:
Code:
while IFS=\| read V1 V2 V3 V4 REST; do [ ! "$V4" = "UPSTR" ] && continue; echo $V1, $V2, $V3; done < file
NOTE1, VALUE1, HMM
NOTE2, , HMP
NOTE3, VALUE3, HML
NOTE4, , HMD
NOTE5, VALUE5, HMS
NOTE6, VALUE6, HSD
NOTE7, , HSS
NOTE8, VALUE8, HEE

However, change the field separator to e.g. : in both your awk script and for the read command and it should fly.


EDIT (for clarity, thanks to jim mcnamara for pointing this out):
However, change the output field separator in your awk script to e.g. : , as well as the IFS for the read command and it should fly:
Code:
awk -F"|" '$4 ~ /UPSTR/{print $1":"$2":"$3}' Input.txt | while IFS=":" read VAR1 VAR2 VAR3 ...


Last edited by RudiC; 12-09-2016 at 11:39 AM.. Reason: Improving clarity (hopefully!)
This User Gave Thanks to RudiC For This Post:
# 3  
Old 12-09-2016
@RudiC -
I'm missing something here. Your script works correctly as posted. I do not see the what you mean with using : as the field separator in order to get the desired result.

Example just with awk:
Code:
 awk -F"|" '$4 ~ /UPSTR/{print $1,","$2,",",$3}' filename
NOTE1 ,VALUE1 , HMM
NOTE2 , , HMP
NOTE3 ,VALUE3 , HML
NOTE4 , , HMD
NOTE5 ,VALUE5 , HMS
NOTE6 ,VALUE6 , HSD
NOTE7 , , HSS
NOTE8 ,VALUE8 , HEE

I get no output from:

Code:
 awk -F ":" '$4 ~ /UPSTR/{print $1,","$2,",",$3}' filename

So what am I not seeing? Thanks
# 4  
Old 12-09-2016
No, I meant ... print $1":"$2":"$3}' Input.txt | while IFS=":" read ... in post#1.

I do admit, though, that my comment was misleading as a back reference was missing. Editing the post to make it clearer. Sorry.
# 5  
Old 12-09-2016
Thanks RudiC..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Scirpt to fetch the variable from sqlplus

Hi Gurus, I am stuck with the step where i need to fetch the location & sales from the below procedure by taking it from table field using the for loop. any idea how this can be done in unix. From one column both the location and sales are taken out. create or replace procedure newyork... (2 Replies)
Discussion started by: arun888
2 Replies

2. UNIX for Advanced & Expert Users

[Solved] Unable to fetch the UNIX variable information

I am having a file called variable_info.ksh in that file I am having following global variable information like… EMAIL_PATH=/var/mail TMP_PATH=/home/tmp And we are having another temporary parameter file abcd.txt, in that file we are having the following information like… EMAIL|EMAI_PATH I... (4 Replies)
Discussion started by: mesahammad
4 Replies

3. Shell Programming and Scripting

How to change variable length field?

Hello, I have a file with a date field with various lengths. For example: m/d/yyyy hh:mm or h:mm mm/dd/yyyy hh:mm or h:mm Is there a way using sed or awk to change the field to m/d/y ? I don't need the hours and minutes in that field, just the date in the proper format. Thanks in... (6 Replies)
Discussion started by: sonnyo916
6 Replies

4. Shell Programming and Scripting

Fetch the different data by searching with a same variable from a file in AIX server

Hi, I am trying to fetch the different values in an xml file by searching with the same variable in AIX Server. <name>SharedResources/Shared/JNDI/Username</name> <value>admin</value> <name>SharedResources/Shared/JNDI/Username</name> ... (1 Reply)
Discussion started by: tejastrikez
1 Replies

5. Shell Programming and Scripting

Need help to get the nth field of the variable

Below is the format of my file which consist of 1)BSB 2)BSB/ADS 3)CIB 4)CIB/CRH 5)CIB/DCC 6)CIB/EMD 7)CIB/GDSPresentation 8)CIB/HCH 9)CIB/HSM 10)CIB/MCH 11)CIB/RCH 12)COB 13)DCI 14)DIB 15)DIB/H2H 16)DIB/HotelSync 17)DIB/UADBA (11 Replies)
Discussion started by: rohit22hamirpur
11 Replies

6. Shell Programming and Scripting

Storing a field within a variable

Hi, I need to figure out a way to to capture the contents of a field that is separated by a pipe sign. Example Data: -100F| some other description -10C| some description | some description As you can see, the length of the field on the left of the pipe can be any length and... (3 Replies)
Discussion started by: doza22
3 Replies

7. UNIX for Dummies Questions & Answers

fetch Variable value

Hi Guys, I have written a script that declares all the variables and its values in a conf file. Now i use a variable whose value i need to change it in one of the sub-file that is used in the script. In the startup file i want to print or check its value. The value get changed and printed... (5 Replies)
Discussion started by: Swapna173
5 Replies

8. Shell Programming and Scripting

How to fetch variable value in if block and to compare it with certain string

Hi, I am trying to execute this command if ; then but getting error .Some problem with reteriving the value of $exception_info. Please help.Its urgent. thanks (4 Replies)
Discussion started by: khushboo
4 Replies

9. Shell Programming and Scripting

Unix code to fetch the first field till space in a variable

Hi, I want to get the value of the first field till space in a variable. e.g x=323 /test/personel/logs/File1 I want to get the first field till space i.e 323 in another variable ,lets say y. echo $x|cut -d' ' -f1 gives 323 but when I'm trying y=`echo $x|cut -d' ' -f1 its giving... (3 Replies)
Discussion started by: autosys_nm
3 Replies

10. Shell Programming and Scripting

Fetch the field from file and add to new file.

Admins Pls. assist me to update a new field having 40 rows to a file. Test1.output LV Space %free Space_used_04_15 dev 500 7 67% dev1 800 7 75% dev2 800 7 75% dev3 900 7 ... (2 Replies)
Discussion started by: maestrohero
2 Replies
Login or Register to Ask a Question