473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Shell script to modify files

365 100+
Looking for shell script to modify file2 below based on first file. Basically I need to look for rows start with "TAPES" in file1, then I have to take 2nd column in that line "LOC" and look for matching word in file2, then edit that line in file2, the third column in file1 in that row tells that column should edit in file2, basically third column tells which column should edit in file, the fourth column in file1 is the replacement in file2 in that column. I should pass "TAPES" to the script and second file name like below. can I get script?.

./script1 file2 TAPES


file1
TAPES|LOC|2|CA
CD|TOP|5|CA
TAPES|LOC|3|PA
CD|TOP|5|CA
TAPES|LOC|5|TX


file2
LOC|2|STATE|NOT|FILL|
LOC|2|PXA|FILL|EMPTY|
LOC|2|STATE|NOT|FILL|
Jan 14 '10 #1
10 4001
ashitpro
542 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. #!/bin/bash
  2. file1='file1' #specify your file1 name
  3. file2=$1      #file2 name
  4. kword=$2      #Word to find in first column of file1
  5.  
  6. for line1 in `cat $file1`
  7. do
  8.         first_word_file1=`echo $line1 | awk '{print $1}' FS="|"` > /dev/null
  9.         if [ $first_word_file1 == $kword ]
  10.         then
  11.                 second_word_file1=`echo $line1 | awk '{print $2}' FS="|"` > /dev/null
  12.                 third_word_file1=`echo $line1 | awk '{print $3}' FS="|"` > /dev/null
  13.                 fourth_word_file1=`echo $line1 | awk '{print $4}' FS="|"` > /dev/null
  14.  
  15.                 echo $third_word_file1 $fourth_word_file1
  16.                 #Create temporary file for future use
  17.                 touch /tmp/temp1221.bf
  18.  
  19.                 #Now search file2 for line starting with $second_word_file1
  20.                 for line2 in `cat $file2`
  21.                 do
  22.                         first_word_file2=`echo $line2 | awk '{print $1}' FS="|"` > /dev/null
  23.                         if [ $second_word_file1 == $first_word_file2 ]
  24.                         then
  25.                                 #Now replace the word coming under column number specified
  26.                                 #by $third_word_file1 by word in $fourth_word_file1
  27.  
  28.                                 target_word=`echo $line2 | awk -v N=$third_word_file1 '{printf("%s",$N)}' FS="|"` > /dev/null
  29.                                 modified_line=`echo $line2 | sed s/\|$target_word\|/\|$fourth_word_file1\|/`
  30.                                 echo $modified_line >> /tmp/temp1221.bf
  31.  
  32.                         else
  33.                                 echo $line2 >> /tmp/temp1221.bf
  34.                         fi
  35.  
  36.                 done
  37.                 rm -rf $file2
  38.                 mv /tmp/temp1221.bf $file2
  39.         fi
  40. done
  41.  
  42.  
This script is strictly based on files you provided..
After running this script my file2 looked like:

LOC|CA|PA|NOT|TX|
LOC|CA|PA|FILL|TX|
LOC|CA|PA|NOT|TX|
Jan 14 '10 #2
tvnaidu
365 100+
Great, thank you verymuch.
Jan 14 '10 #3
tvnaidu
365 100+
Can I add random number for every entry which I modifled in file2 (append random number into the entry like below).

LOC|CA|PA|NOT|TX| ----> LOC|CA1234|PA|NOT|TX|
LOC|CA|PA|FILL|TX| -----> LOC|CA1234|PA4567|NOT|TX|
LOC|CA|PA|NOT|TX| -------> LOC|CA1234|PA4567|NOT|TX3456|
Jan 14 '10 #4
ashitpro
542 Expert 512MB
RANDOM=`date '+%s'`

if you echo $RAMDOM you will get random number for each echo call...
Add this statement to top of the script
And change line number 29 to
Expand|Select|Wrap|Line Numbers
  1. modified_line=`echo $line2 | sed s/\|$target_word\|/\|$fourth_word_file1$RANDOM\|/`
Jan 15 '10 #5
tvnaidu
365 100+
thank you verymuch, appreciated
Jan 15 '10 #6
tvnaidu
365 100+
I added RANDOM into the program, it always generates same number, I need to append different number for each replacement. Is there anyway I can get different number usiong RANDOM everytime?.

Now I gets 21468 for RANDOM. thanks in advance.
Jan 16 '10 #7
tvnaidu
365 100+
When I run this script for a large original file, I am getting below error for line 23, any idea?, I was referring internet for this kind of error, some places says, "=" should be "==", but in this case, it is comparing those two words with "==", what else could be?.

Line 23: [: LOC: Unary operator expected


if [ $second_word_file1 == $first_word_file2 ]
Jan 16 '10 #8
ashitpro
542 Expert 512MB
I tried this at shell:
RANDOM=`date '+%s'`

later for each call of
echo $RANDOM

I had got random number, I am not sure what is going wrong in your case.
Try initializing RANDOM variable just above line 29, instead of at the top.
Jan 18 '10 #9
ashitpro
542 Expert 512MB
change this line:
if [ $second_word_file1 == $first_word_file2 ]
to:
if [ "$second_word_file1" = "$first_word_file2" ]

You are having empty strings in either second column of file1 or first column of file2, recheck the files. I haven't tested or written the script considering the emptiness of column.
Jan 18 '10 #10
tvnaidu
365 100+
Thank you verymuch, appreciated. Let me try.
Jan 18 '10 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Will Seay | last post by:
At the end of this message I've pasted a script we're trying to modify slightly. I don't believe it is VBscript or javascript but these are the closest groups I could find with my limited...
9
by: Jeff Wagner | last post by:
I have a project of converting numerous DOS cmd shell scripts to Python. Is there a tutorial to getting started? Thanks, Jeff
7
by: Frank Potter | last post by:
I learned some python in windows. And now I've turned to linux. I read a book and it teaches how to write shell script with bash, but I don't feel like the grammar of bash. Since I know about...
4
by: Anastasios Hatzis | last post by:
I'm looking for a pattern where different client implementations can use the same commands of some fictive tool ("foo") by accessing some kind of API. Actually I have the need for such pattern for...
2
by: jonathan184 | last post by:
Hi I am trying to create a shell script that will look for a contracthead file first and if the contract head file does not exist on day1 exit script. Now on day2 if contracthead exists or...
1
by: Svenn Are Bjerkem | last post by:
Hi, as a user on a linux system I am member of the groups "users" and "design" with users as my default group. To controll the accessibility of some parts of the file system, creation of files and...
1
by: jrw133 | last post by:
So im working on a lab for my unix class and im having some problems with this shell script we are supposed to do here is the question: Write a shell script that accepts a list of files(space...
16
by: pereges | last post by:
Do you see anything wrong about this method ? For eg. I write a shell script a.sh containing : cc -o test file1.c file2.c file3.c and then execute the shell script ( sh a.sh) to compile and...
3
by: Gros Bedo | last post by:
Hello :-) I have a question about Python and Linux shell. I have a python program which is permanently resident in the end-user system. I'm currently producing a RPM package, and it works nicely....
1
by: looza | last post by:
Hi All, I have a bunch of shell script files that use a common shell script file that contains certain global variables that are declared and initialized or derived by some arithmatic. I have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.