Shell script to get one column value(s)


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Shell script to get one column value(s)
# 1  
Old 02-23-2016
Wrench Shell script to get one column value(s)

Hello Gurus,

I have limited experience in writing UNIX scripting. Have business requirement to get one column data from a file. My file format is,

Code:
FileName: /root/dir1/scripts/DSList.txt

colA | colB
SEQ_A | JOB_1a
SEQ_B | JOB_1b
SEQ_B | JOB_2b
SEQ_B | JOB_3b
SEQ_C | JOB_1c

From above format, read the file where colA = ${variable_name} then get colB values display one-by-one using FOR loop.

Please advise me how to write for loop by checking the first field with a variable name.

Thanks in advance.

- Venkat

Last edited by nvkuriseti; 02-23-2016 at 12:00 PM..
# 2  
Old 02-23-2016
Please use code tags as reuqired by forum rules!

Try (untested)
Code:
awk -F\| -vSL=${variable_name} '$1 == SL {print $2}' file

# 3  
Old 02-23-2016
Thank you RudiC. Is there alternative that I can pass this code by For-loop as well?
# 4  
Old 02-23-2016
I don't understand. What keeps you from using command substitution?
# 5  
Old 02-23-2016
Small change to RudiC's suggestion, otherwise it will not work...
Code:
awk -F'[ \t]*[|][ \t]*' -v SL=${variable_name} '$1 == SL {print $2}' file

Or you could try this:
Code:
while IFS="|$IFS" read colA colB
do 
  if [ "$colA" =  "$variable_name" ]; then
    echo "$colB"
  fi
done < file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to insert data into black column( Secound Column ) in excel (.XLSX) file using shell script?

Source Code of the original script is down below please run the script and try to solve this problem this is my data and I want it column wise 2019-03-20 13:00:00:000 2019-03-20 15:00:00:000 1 Operating System LAB 0 1 1 1 1 1 1 1 1 1 0 1 (5 Replies)
Discussion started by: Shubham1182
5 Replies

2. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

3. Shell Programming and Scripting

Vlookup using Ask from specific column shell script

Input file1 frame1,dummy,server1, 00C1C N/A RDF1+TDEV RW 51789 frame1,dummy,server1, 00C1D N/A RDF1+TDEV RW 51789 frame1,dummy,server1, 00C1E N/A RDF1+TDEV RW 51789 frame1,dummy,server1, 00C1F N/A RDF1+TDEV RW 51789 frame1,dummy,server1, 00C20 N/A RDF1+TDEV RW 51789 frame1,dummy,server1,... (10 Replies)
Discussion started by: ranjancom2000
10 Replies

4. Shell Programming and Scripting

Displaying Column header in shell script

Hi, I need to display specific columns using select statement and spooled to a file and sending it as e-mail. But i am not seeing column header in my output even i use SET HEADING ON.//PREDEFINED LOGIN DETAILS ${ORACLE_HOME}/bin/sqlplus -s ${DB_LOGIN}/${DB_PASSWD} <<EOF SET FEEDBACK OFF SET... (1 Reply)
Discussion started by: pvelmuru
1 Replies

5. UNIX for Dummies Questions & Answers

Modify Column Data using Shell Script

HI Guys, Input :- P081 wr1 12p0d5: 22.8 P081 wr1 12p2d18: 23.1 P149 wr1 1pxcud6/port_0_dev_7: 20.4 P149 wr1 1pxcud4/port_1_dev_10: 22.4 OutputP081 wr1 120 22.8 P081 wr1 122 23.1 P149 wr1 10 20.4 P149 wr1 11 22.4 In in First two line delete p and after d untill : In Last two line... (4 Replies)
Discussion started by: pareshkp
4 Replies

6. Homework & Coursework Questions

Shell Script: Sorting by column using grep/awk

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: You will write a script that will read a tab-separated file that contains the names of all 50 states &... (7 Replies)
Discussion started by: tburns517
7 Replies

7. Shell Programming and Scripting

shell script to print particular column into text file

hi everyone, i need a script which would just print column into a text file nmap 10.226.112.222 PORT STATE SERVICE 7/tcp open echo 13/tcp open daytime 22/tcp open ssh 23/tcp open telnet 37/tcp open time 1100/tcp open unknown above nmap command gives us open ports on that IP. can... (6 Replies)
Discussion started by: anand121
6 Replies

8. Shell Programming and Scripting

shell script to sort the 5th column

hi folks, I have this data in a data.txt file and i want to sort the 5th column and in descending order: Jun 15 119.167.247.40 = 23 Jun 15 119.167.247.40 = 3 Jun 15 208.115.46.125 = 12 Jun 15 208.115.46.125 = 6 Jun 15 210.51.10.160 = 20 I want this sample output: Jun... (2 Replies)
Discussion started by: linuxgeek
2 Replies

9. Shell Programming and Scripting

To update a column in a table through shell script

Hi All, I need to write a shell script in UNIX that should accept booking number as an argument and update it with value "NULL" if the transaction date is greater than 2 years. Booking number and transaction_date are the two columns of the table table_booking. Something like this, through... (3 Replies)
Discussion started by: shilpa_acc
3 Replies

10. Shell Programming and Scripting

updating a column in oracle table using shell script

Hi friends, i am having a variable declared in .profile.i am changing its value in a shell script and then i am connecting to oracle and then from there i am calling a .sql called update.sql STATUS is the variable declared in the .profile =============================== if sqlplus <<END... (3 Replies)
Discussion started by: sveera
3 Replies
Login or Register to Ask a Question