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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash shell script not working-picking segment patterns from a file
# 1  
Old 10-12-2015
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:
Code:
TS3*1451575*12*20151231*4*482.44
NM1*QC*1*CUTLER*BETTY
DTM*472*20150808
REF*6R*00000802960404783585
NM1*QC*1*CUTLER*BETTY
DTM*472*20150808
REF*6R*00000802960301783584
NM1*QC*1*POWELL*XYZ
DTM*472*20150808
REF*6R*00000802905105783586
NM1*QC*1*QUIRING*ROBERT
DTM*472*20150808
REF*6R*00000803057901783583
TS3*1459088*12*20151231*2*156.8
NM1*QC*1*VENABLE*DAVID
DTM*472*20150808
REF*6R*00000882310302783651
NM1*QC*1*WHITING*SHERRY
DTM*472*20150808
REF*6R*00000882461300783652

Code:
 
Script:
#!/bin/sh

echo "Please enter file name"

read file
 

set first = ""
set second = ""
set third = ""
set fourth = ""
set fifth = ""


while read line

do

line_check=$(echo $line|awk -F"\*" '{print $1}')
if [ $line_check == "TS3" ]

then

set first  =  $line

elif [ $line_check = "CLP" ]

then

set second = $line


elif [ $line_check = "NM1" ]

then

set third = $line


elif [ $line_check = "DTM" ]

then

set fourth = $line


else [ $line_check = "REF*" ]

set fifth = $line

if [ $first != " -a $second != "" -a $third != "" -a $fourth != "" -a $fifth != "" ]
then
echo $first >> newfile.txt
echo $second >> newfile.txt
echo $third  >> newfile.txt
echo $fourth >> newfile.txt
echo $fifth  >> newfile.txt

set first = ""
set second = ""
set third = ""
set fourth = ""
set  fifth = ""

fi
fi
done < $file


Last edited by Franklin52; 10-12-2015 at 10:36 AM.. Reason: Please use code tags
# 2  
Old 10-12-2015
If you have GNU grep available to you the following performs the required check
Code:
#!/bin/sh

echo "Please enter file name"

read filename

egrep -no '^TS3|^NM1|^DTM|^REF|^CLP' filename

# 3  
Old 10-12-2015
Thanks for the help.but egrep is not available on this machine.

btw Do you see any syntax errors in my script?
# 4  
Old 10-12-2015
try grep -e ?

the issue is that you're attempting to read the file from STDIN, you're also re-writing a very specific instance of grep.
# 5  
Old 10-12-2015
What exactly is "not working"? Error messages? Empty result files? Dth. else?

---------- Post updated at 19:29 ---------- Previous update was at 19:19 ----------

Plus, what system & shell are you using?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Passing Arguments to shell script from file is not working as expected.

Hi All, I have below simple shell script in cloudera quick start vm cenos 6 which copy file from source to destination. # file_copy.sh source_dir = ${source_dir} target = ${target_dir} cp source_dir target and my parameter file is like below #parameter_file.txt source_dir =... (4 Replies)
Discussion started by: Narasimhasss
4 Replies

2. Shell Programming and Scripting

Bash: Picking up filenames then moving to next directory

I have a collection of directories, for example as below I want to create a loop that goes in the first directory and picks up the *hhz*.sac.pzs filename in a variable, and the other files matching *hhz*.sac in another variable (however I do not want to pick the *hhz*.sac.pzs). This is because... (4 Replies)
Discussion started by: kristinu
4 Replies

3. Shell Programming and Scripting

Bash - Find files excluding file patterns and subfolder patterns

Hello. For a given folder, I want to select any files find $PATH1 -f \( -name "*" but omit any files like pattern name ! -iname "*.jpg" ! -iname "*.xsession*" ..... \) and also omit any subfolder like pattern name -type d \( -name "/etc/gconf/gconf.*" -o -name "*cache*" -o -name "*Cache*" -o... (2 Replies)
Discussion started by: jcdole
2 Replies

4. Shell Programming and Scripting

Aliases NOT working inside bash shell script

i have defined a function ln_s() for customizing the ln command in script1.sh. more script1.sh echo "Starting Execution" ./script2.sh echo "End of Execution" ln_s(){ ] && return ln -s "$1" "$2" } My script1.sh executes another script2.sh which has the following entry more script2.sh... (12 Replies)
Discussion started by: mohtashims
12 Replies

5. Shell Programming and Scripting

Linux/bash Script only working if executed from shell prompt

Hi, maybe I'm asking a VERY dumb question, but would anybody out there tell me, why this f****** script won't work if executed as a cronjob, but works fine if executed from a shell prompt? #! /bin/bash set PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin date >>... (3 Replies)
Discussion started by: beislhur
3 Replies

6. Shell Programming and Scripting

export not working in Bash shell

Hi Friends, I am presently migrating shell scripts writter in KSH to SH.I am stuck at this place and i am not able to find a work around:- Let the script name is x.sh Below are some of the codes in it... export abc=hello export abc=hi export abc=how When i am trying to compile the script ... (6 Replies)
Discussion started by: amit.behera
6 Replies

7. UNIX for Advanced & Expert Users

Working in new IP segment, But Product configured with another IP??

Dear all, We have installed one of our product in a HP PC server(DL380G5)/SUSE 10 SP1 (2 network cards) with an IP address 10.18.7.25. The Product is working fine. Now for giving a internet based demo, we have moved to a different network(192.168.50.*) For the demo, we want to use... (0 Replies)
Discussion started by: jagannathks
0 Replies

8. Shell Programming and Scripting

Picking high and low variables in a bash script - possible?

Is it possible to have a bash script pick the highest and lowest values of four variables? I've been googling for this but haven't come up with anything. I have a script that assigns variables ($c0, $c1, $c2, and $c3) based on the coretemps from grep/sed statements of sensors. I'd like to also... (5 Replies)
Discussion started by: graysky
5 Replies

9. Shell Programming and Scripting

shell script to format file based on specific patterns

Please help me out and drag me out the deadlock I am stuck into: I have a file. I want the statements under a if...then condition be listed in a separate file in the manner condition|statement.Following are the different input pattern and corresponding output parameters.any generic code to... (7 Replies)
Discussion started by: joyan321
7 Replies

10. Shell Programming and Scripting

if loop not working in BASH shell

i have this code for a simple if loop: #!/bin/bash array="1 2 3 4 5" array2="5 6 7 8 9" if } -gt ${array} ]; then echo "${array2} is greater than ${array}!!" fi the error is ./script8: line 9: [: too many arguments ./script8: line 9: [: too many arguments ./script8: line 9: [:... (10 Replies)
Discussion started by: npatwardhan
10 Replies
Login or Register to Ask a Question