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|
10 3996 -
#!/bin/bash
-
file1='file1' #specify your file1 name
-
file2=$1 #file2 name
-
kword=$2 #Word to find in first column of file1
-
-
for line1 in `cat $file1`
-
do
-
first_word_file1=`echo $line1 | awk '{print $1}' FS="|"` > /dev/null
-
if [ $first_word_file1 == $kword ]
-
then
-
second_word_file1=`echo $line1 | awk '{print $2}' FS="|"` > /dev/null
-
third_word_file1=`echo $line1 | awk '{print $3}' FS="|"` > /dev/null
-
fourth_word_file1=`echo $line1 | awk '{print $4}' FS="|"` > /dev/null
-
-
echo $third_word_file1 $fourth_word_file1
-
#Create temporary file for future use
-
touch /tmp/temp1221.bf
-
-
#Now search file2 for line starting with $second_word_file1
-
for line2 in `cat $file2`
-
do
-
first_word_file2=`echo $line2 | awk '{print $1}' FS="|"` > /dev/null
-
if [ $second_word_file1 == $first_word_file2 ]
-
then
-
#Now replace the word coming under column number specified
-
#by $third_word_file1 by word in $fourth_word_file1
-
-
target_word=`echo $line2 | awk -v N=$third_word_file1 '{printf("%s",$N)}' FS="|"` > /dev/null
-
modified_line=`echo $line2 | sed s/\|$target_word\|/\|$fourth_word_file1\|/`
-
echo $modified_line >> /tmp/temp1221.bf
-
-
else
-
echo $line2 >> /tmp/temp1221.bf
-
fi
-
-
done
-
rm -rf $file2
-
mv /tmp/temp1221.bf $file2
-
fi
-
done
-
-
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|
Great, thank you verymuch.
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|
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 -
modified_line=`echo $line2 | sed s/\|$target_word\|/\|$fourth_word_file1$RANDOM\|/`
thank you verymuch, appreciated
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.
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 ]
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.
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.
Thank you verymuch, appreciated. Let me try.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
|
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...
| |