473,327 Members | 2,071 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,327 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 3996
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.