Help with ksh-to read ip file & append lines to another file based on pattern match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with ksh-to read ip file & append lines to another file based on pattern match
# 1  
Old 07-15-2013
Help with ksh-to read ip file & append lines to another file based on pattern match

Hi,

I need help with this-
input.txt :
L B white
X Y white
A B brown
M Y black

Read this input file and if 3rd column is "white", then add specific lines to another file insert.txt.
If 3rd column is brown, add different set of lines to insert.txt, and so on.
For example, the given set of lines if 3rd column is white is
"*colors are random: yes
*colors are light: yes"

If 3rd column is brown, the lines to be appended are
"*colors are dark: no
*colors are random: no:

insert.txt is not empty, so the newly added lines have to be appended to end of file.
I prefer to have this in ksh.

Thanks!
# 2  
Old 07-15-2013
Is this homework?
What do you have so far?
# 3  
Old 07-16-2013
I have one which works but is not dependent on column3 in my input file.
Code:
while read col1 col2 col3
do
  my set of of instructions
done < input.txt

But when i edit the ksh script so that it matches 3rd column, it does not work.Also, I am not sure how to append to a file, once the column matches.
#!/bin/ksh
perl -e 'print "#Added for ...."' >> dir/my.txt

Code:
while read col1 col2 col3
do
echo $col3
if [["$col3"="white"]]; then
        print " matched"
else
        print "not matched"
fi
done < input.txt


Last edited by Scott; 07-16-2013 at 02:24 AM.. Reason: Please use code tags
# 4  
Old 07-16-2013
The [[ ]] or the portable [ ] require spaces:
Code:
while read col1 col2 col3
do
echo $col3
if [ "$col3" = "white" ]; then
        print "white"
elif [ "$col3" = "brown" ]; then
        print "brown"
else
        print "not matched"
fi
done < input.txt

Please embed code in "code" tags! (Look at the top of the WIKI editor)
# 5  
Old 07-16-2013
Try
Code:
while read col1 col2 col3 rest
   do
   case $col3 in
     (white) printf "%s\n%s\n" "*colors are random: yes" "*colors are light: yes";;
     (brown) printf "%s\n%s\n" "*colors are dark: no" "*colors are random: no:" ;;
     (*)     continue;;
   esac
   done <input.txt >>insert.txt

This User Gave Thanks to RudiC For This Post:
# 6  
Old 07-17-2013
Thanks for the replies.
The if loop with space worked.
And, for the 2nd part of my original question, I used "cat >> "to write into file.
Thanks again!
# 7  
Old 07-17-2013
Yes >> is "append to file", but cat is not needed.
Code:
print "white" >> insert.txt

or, as Rudi suggested, redirect the output of the whole loop
Code:
while ...
do
...
done >> insert.txt

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to append multiple text files into one file based on pattern present in filaname

Hi All-I am new to Unix , I need to write a script. Can someone help me with a requirement where I have list of files in a directory, I want to Merge the files if a pattern of string matches in filenames? AAAL_555A_ORANGE1_F190404.TXT AAAL_555A_ORANGE2_F190404.TXT AAAL_555A_ORANGE3_F190404.TXT... (6 Replies)
Discussion started by: Shankar455
6 Replies

2. UNIX for Beginners Questions & Answers

Match pattern only between certain lines in entire file

Hello, I have input that looks like this: * 0 -1 103 0 0 m. 7 LineNr 23 ClauseNr 1: 1: 1: 304: 0 0 SentenceNr 13 TxtType: Q Pargr: 2.1 ClType:MSyn PS004,006 ZBX= 0 1 1 0 7 -1 -1 3 2 3 2 -1 1 1 -1 -1 -1 -1 0 501 0 PS004,006 ZBX ... (2 Replies)
Discussion started by: jvoot
2 Replies

3. Shell Programming and Scripting

awk to update value based on pattern match in another file

In the awk, thanks you @RavinderSingh13, for the help in below, hopefully it is close as I am trying to update the value in $12 of the tab-delimeted file2 with the matching value in $1 of the space delimeted file1. I have added comments for each line as well. Thank you :). awk awk '$12 ==... (10 Replies)
Discussion started by: cmccabe
10 Replies

4. Shell Programming and Scripting

Match all lines in file where specific text pattern is less than

In the below file I am trying to grep or similar, all lines where only AF= is less than 0.4.. Thank you :). grep grep "AF=" ,+ .4 file file 12 112036782 . T C 34.0248 PASS ... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

Obtain pattern from file; Append 1st Match

Not clear how to do so. Looking to append the 1st match of said pattern with 'OK TO REMOVE' file containing patter File1.txt RMS_QUANTITY_RT SMS_QUANTITY_RT file to search File2.txt <!-- dec=664, SMS_QUANTITY_RT --> <!-- dec=664, RMS_QUANTITY_RT --> Projected Results <!--... (3 Replies)
Discussion started by: TY718
3 Replies

6. Shell Programming and Scripting

Match and Append Based on file contexts

Not Sure how to do this. Some combo of awk and sed perhaps. If String in File1 match String in file2 then append file2 File1.txt BullTerrier Boxer Bulldog File2.txt <Defined info="AllAnimals" group="Adoptions" setting="animals"> <SomeID ="NumbersRepresentingDogName"> <for> <add... (2 Replies)
Discussion started by: TY718
2 Replies

7. Shell Programming and Scripting

Delete lines based on pattern match

BASH in Solaris 10 I have a log file like below. Whenever the pattern ORA-39083 is encountered, I want to delete the line which has this pattern and 3 lines below it. $ cat someLogfile.txt ORA-39083: Object type OBJECT_GRANT failed to create with error: ORA-01917: user or role 'CMPA' does... (4 Replies)
Discussion started by: kraljic
4 Replies

8. Shell Programming and Scripting

deleting lines in a file that match a pattern without opening it

In Unix, how do I delete lines in a file that match a particular pattern without opening it. File contents are foo line1 misc whatever foo line 2 i want to delete all lines that have the pattern "foo" without opening the file. File should eventually contain misc whatever (1 Reply)
Discussion started by: osbourneric
1 Replies

9. Shell Programming and Scripting

Merge two file data together based on specific pattern match

My input: File_1: 2000_t g1110.b1 abb.1 2001_t g1111.b1 abb.2 abb.2 g1112.b1 abb.3 2002_t . . File_2: 2000_t Ali england 135 abb.1 Zoe british 150 2001_t Ali england 305 g1111.b1 Lucy russia 126 (6 Replies)
Discussion started by: patrick87
6 Replies

10. Shell Programming and Scripting

read from a specific pattern from one file and append it to another

Hi! Everyone, Say this file1 -------------- line 1 51610183 420001010 0010CTCTLEDPPOO 2151610183 line 2 2151610183 420001010 0030A2TH2 line 3 2151610183 420001010 0040A2TH3 line 4 2151610183 420001010 ... (0 Replies)
Discussion started by: kinkar_ghosh
0 Replies
Login or Register to Ask a Question

Featured Tech Videos