Picking values from a file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Picking values from a file.
# 1  
Old 08-21-2012
Picking values from a file.

Hi,

I have a file which contains values in this format.
Code:
abc
cde
fgh
ijk
lmn
opq
rst
uvw

The user will pass the required parameter from the command line.
My requirement is that script should pick the values passed by the user and the next value in the next line.

Like if the user passes abc then the script must pick the value cde also and store it in two separate variables like x=abc and y=cde.

please suggest how to do this?Smilie


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-21-2012 at 08:33 AM.. Reason: code tags
# 2  
Old 08-21-2012
Can you show us what have you tried so far and where you are stuck?
# 3  
Old 08-21-2012
haven't tried anything yet.getting no idea how to do this.Smilie
# 4  
Old 08-21-2012
Excuse me, but you worked with getopts for example in this thread https://www.unix.com/shell-programmin...m-getopts.html and don't know even how to start with this?

Please show some effort first - answers before that will be moderated.
# 5  
Old 08-21-2012
store the value passed in one variable say x then use

Code:
y=$( awk -v val=$x '(/val/){getline;print $1}' filename   )

# 6  
Old 08-21-2012
And what should happen if the passed word is not present it the list..?
# 7  
Old 08-21-2012
Code:
#!/bin/sh
# The usage of this script.
usage="Usage is $0"
usage="$usage [-a <file>]"
usage="$usage [-b <pass_word>]"
usage="$usage [-c <data>]"
 
if [ $# == 0 ]; then
echo "Invalid script invocation"
echo $usage
exit 1
fi
if [ $# != 3 ];then
echo "Not enough parameters passed."
echo $usage
exit !
fi
 
# Use the getopt utility to set up the command line flags.
set -- `/usr/bin/getopt a:b:c:d: $*`
# Process individual command line arguments
while [ $1 != -- ]; do
case $1 in
 
-a) dumpfile=$2
shift
;;
-b) word_passed=$2
shift
;;
-c) database=$2
shift
;;
 
esac
shift
done
 
x=`grep -ci 'word_passed' test.txt`

This is where i am struck.
After finding the word how to pick up the word from next line.

If the word is not present then it will throw an error message.

Last edited by Franklin52; 08-21-2012 at 08:58 AM.. Reason: Please use code tags for data and code samples
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Picking the latest file based on a timestamp for a Dynamic file name

Hi , I did the initial search but could not find what I was expecting for. 15606Always_9999999997_20160418.xml 15606Always_9999999998_20160418.xml 15606Always_9999999999_20160418.xml 9819Always_99999999900_20160418.xml 9819Always_99999999911_20160418.xmlAbove is the list of files I... (4 Replies)
Discussion started by: chillblue
4 Replies

2. Shell Programming and Scripting

Bash shell script not working-picking segment patterns from a file

Hi All, I have to pick particular segments from a file and I have prepared below shell script.But its not working and I am not able to find out whats the issue.could you guys pls help? Sample file: TS3*1451575*12*20151231*4*482.44 NM1*QC*1*CUTLER*BETTY DTM*472*20150808... (4 Replies)
Discussion started by: Venkata Prasad
4 Replies

3. Programming

Python:Picking values from a table

Hi I trying to write a python script that executes a command to screen scrub results below I will appreciate it very much if you can help me with a python script that can pick the percentage USAGE in the second column based on the supplied queue number in the first column Thanks in advance. ... (2 Replies)
Discussion started by: kaf3773
2 Replies

4. UNIX for Dummies Questions & Answers

Sorting files based on timestamp and picking the latest file

Hi Friends, Newbie to shell scripting Currently i have used the below to sort data based on filenames and datestamp $ printf '%s\n' *.dat* | sort -t. -k3,4 filename_1.dat.20120430.Z filename_2.dat.20120430.Z filename_3.dat.20120430.Z filename_1.dat.20120501.Z filename_2.dat.20120501.Z... (12 Replies)
Discussion started by: robertbrown624
12 Replies

5. Shell Programming and Scripting

Urgent ...pls Sorting files based on timestamp and picking the latest file

Hi Friends, Newbie to shell scripting. Currently i have used the below to sort data based on filenames and datestamp $ printf '%s\n' *.dat* | sort -t. -k3,4 filename_1.dat.20120430.Z filename_2.dat.20120430.Z filename_3.dat.20120430.Z filename_1.dat.20120501.Z filename_2.dat.20120501.Z... (1 Reply)
Discussion started by: robertbrown624
1 Replies

6. Shell Programming and Scripting

Picking contents value of file at run time based on variable values

HI, I have to write a unix script and need your help. in my application where I have to invoke this script a varialble is there where the value comes in a variable . for example variable can be var=ABC like this there will be any type of value in the vcariable. there is a unix directory... (2 Replies)
Discussion started by: manish8484
2 Replies

7. Shell Programming and Scripting

picking file from a particular URL and the processing

body { margin: 0 0 0 0; padding:0 0 0 0 }td,div { font-family:Courier New;font-size:10pt;vertical-align:top }body { margin: 0 0 0 0; padding:0 0 0 0 }.transcript { background-color:#d2d2d2; }.messageBlock { margin-left:4px; margin-bottom:3px }.message { margin-left:100px; word-wrap:break-word;... (0 Replies)
Discussion started by: priyanka3006
0 Replies

8. Shell Programming and Scripting

parsing data file picking out certain fields

I have a file that is large and is broken up by groups of data. I want to take certain fields and display them different to make it easier to read. Given input file below: 2008 fl01 LAC 2589 polk doal xx 2008q1 mx sect 25698541 Sales 08 Dept group lead1 ... (8 Replies)
Discussion started by: timj123
8 Replies

9. Shell Programming and Scripting

Picking the file based on Date..Requirement

Dear frnds My requirement is as follows -rw-r----- 1 f02 dd 109428250 May 18 14:02 Extracts_20070518104730.zip -rw-r----- 1 f02 dd 109493187 May 21 13:30 Extracts_20070521091700.zip -rw-r----- 1 f02 dd 109993058 May 23 14:14 Extracts_20070523085955.zip -rw-r----- 1... (5 Replies)
Discussion started by: sureshg_sampat
5 Replies

10. UNIX for Dummies Questions & Answers

Picking out text from one file and writing it to another.

Hello. I have one file that is a collection of discarded emails. Each email is it's own section with each section beginning with the same header (ie 'Another Email' ). I want to traverse through the file and every time I find the header ('Another Email') I then want to pick out the 'To:' line... (5 Replies)
Discussion started by: scottf33
5 Replies
Login or Register to Ask a Question