Parse file and copy value to variable in sh script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse file and copy value to variable in sh script.
# 1  
Old 05-06-2010
Parse file and copy value to variable in sh script.

Hi,

I need to parse a simple text file like below and store the word that starts with BR* to a variable say $BRno. I need to do this in sh script.

NOTE: the length of the numbers following BR is not constant (eg: it could be BR1234 or BR22233). And there is only 1 BRxxxxx in a file at a given time.

.txt file:

BR276828
Apple
Orange
CHERRY
test.txt

Gurus, Please help as soon as you can !

Thanks !

*************************
This is not a homework. I am working on a few automation scripts at work. My manager just added a few simple requirements at the last minute today. This is one of them. And I need to roll-out the scripts tomorrow. I need to get a return value from a perl script to a sh script(calling the perl script from here). So I was tyring this method for that: copying the output of the perl script to a file, and trying to parse that file from the sh script to get the BRXXX to a variable that I will print out.

This is all probably confusing, that's why I simply just asked what I need without the background. Sorry about that !

Thanks fo any help !

********************

Last edited by script2010; 05-06-2010 at 09:52 PM..
# 2  
Old 05-06-2010
Code:
BRno=$(grep '^BR' filename)
echo $BRno

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 05-06-2010
Thank you so much. it worked !
# 4  
Old 05-06-2010
var=`sed -n '/^BR/{p;q}' file.in`
echo $var

---------- Post updated at 08:46 PM ---------- Previous update was at 08:44 PM ----------

var=`sed -n '/^BR/{p;q}' file.in`
echo $var
This User Gave Thanks to yx07004212 For This Post:
# 5  
Old 05-10-2010
After I find the string, I need to remove 'BR' from it, and just store the numbers following 'BR'

The following worked for .pl script. How can I change it to work in shell script ?

$BR = substr($BRNo, 2, 7)

If BRno is BR123457
# 6  
Old 05-10-2010
Something like: (easy to expnad more settings, not only BR)
Code:
cat file | while read line
do
        BR=""
        case "$line" in
                BR*) BR=${line:2} ;;
        esac
        [ "$BR" != "" ] && echo "BR $BR"
done

This User Gave Thanks to kshji For This Post:
# 7  
Old 05-10-2010
Thanks will try this.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grepping for one variable while using awk to parse an associated variable

Im trying to search for a single variable in the first field and from that output use awk to extract out the lines that contain a value less than a value stored in another variable. Both the variables are associated with each other. Any guidance is appreciated. File that contains the... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

2. Homework & Coursework Questions

Script parse file

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: Hi all, I need help for a script that pulls out a series of numbers from a file (attached file) Basically I... (1 Reply)
Discussion started by: gianvitolinuxs
1 Replies

3. Shell Programming and Scripting

How to copy script output to a variable using same script?

I'm trying to copy script output and use it in this same script as a variable, and call the variable when script is compiled. The script is below. #!/bin/bash output=$(script) while read line; do if ]; then grep "$line" logfile.txt # Create text file echo "From: IT ... (4 Replies)
Discussion started by: SysAdminRialto
4 Replies

4. Shell Programming and Scripting

How to parse the variable in .mk file?

Hi all, I am passing a variable in .mk file as NEED="TEST=Name WORK=Ps DEL=let" I need to echo and export each variable like TEST, WORK. DEL TEST=Name WORK=Ps DEL=let We have to parse the NEED variable which may contain many elements like TEST, DEL& etc.. Any idea on this... (10 Replies)
Discussion started by: ricky-row
10 Replies

5. Shell Programming and Scripting

How to parse xml file in variable-string?

In the wake of the post: how-parse-following-xml-file Thank you for the very useful chakrapani response 302355585-post4 ! A close question. How to pass a file to xmllint in variable? For example, let it be: NEARLY_FILE='<?xml version="1.0" encoding="iso-8859-1"?><html><set label="09/07/29"... (0 Replies)
Discussion started by: OleM2k
0 Replies

6. Shell Programming and Scripting

Parse config file data to script variable

I have a script with few pre defined variables. Also have a config file. Something like this. # config file # Define Oracle User MOD1_TAG=abcde MOD2_TAG=xyzabc MOD3_TAG=def I need to parse the config file and have each of the value copied to different variables. Please suggest what... (1 Reply)
Discussion started by: souryadipta
1 Replies

7. Shell Programming and Scripting

Shell Script to Search for a particular String and copy the timestamp to a variable

Hi, We Perfrom Loads to the database through a Perl script which generates a statistics file. I need to read the statistics. the Statistics file looks something like below: Process Beginning - 08-26-2010-23.41.47 DB2 CONNECTION SUCCESSFUL! Ready to process and load file: FILENAME # of... (2 Replies)
Discussion started by: Praveenkulkarni
2 Replies

8. Shell Programming and Scripting

Making script show command (e.g. copy) being executed and variable substitution?

When script is running you only see when some of the commands are not successfull. Is there a way to see which command are executed and to show the substitution of variables as every line is executed ? (3 Replies)
Discussion started by: gr0124
3 Replies

9. Shell Programming and Scripting

Parse text file in shell & store to variable

Hi, I need to parse a simple text file like below and store the word that starts with BR* to a variable say $BRno. I need to do this in sh script. NOTE: the length of the numbers following BR is in constant. And there is only 1 BRXXX in a file at a given time. .txt file: BR276828... (1 Reply)
Discussion started by: script2010
1 Replies

10. Shell Programming and Scripting

How do you parse a variable in a bash script?

I have a script I use on my web server (Apache2). I am changing to Lighttpd and need to make a few changes. This is what I use on my apache server #!/bin/bash # accepts 3 parameters: <domain name> <user name> <XXXXXXXX> # domain name is without www (just domain.com) # username would be... (3 Replies)
Discussion started by: vertical98
3 Replies
Login or Register to Ask a Question