Numbering, copying and replacing strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Numbering, copying and replacing strings
# 1  
Old 09-14-2011
Numbering, copying and replacing strings

hello everybody there, I'm a new bash shell programmer and I'm dealing with a problem.
To start, I have a file with a number of string lines which may contain a particular string or not. I have to write a code that identifies the line containing one particular string and keeps it, but also writes another line in which that string is replaced by another one.
For example, the input file is:

Code:
1 AND
2 HOT
3 WITH
4 HOUSE
ecc

and I want that each time there is the letter "H" in a line the code should rewrite the word containing the H and also create a new line with the same word containing the letter "M" instead of "H", so it should be:
Code:
1 AND
2 HOT
3 MOT
4 WITH
5 WITM
6 HOUSE
7 MOUSE

I wrote a simple code but it only replaces the string:
Code:
#!/bin/bash
OLD="H"
NEW="M"

DPATH="/home/me/file.dat"
BPATH="/home/me/out.dat"
TFILE="/tmp/out.tmp.$$"
[ ! -d $BPATH ] && mkdir -p $BPATH || :
for f in $DPATH
     do
          if [ -f $f -a -r $f ]; then
          /bin/cp -f $f $BPATH
          sed "s/$OLD/$NEW/g" "$f" > $TFILE && mv $TFILE "$f"
     else
          echo "Error: Cannot read $f"
     fi
done

#/bin/rm $TFILE

and I don't know how to implement the other points!
Hope I was clear enough!
Thanks to anyone helping!

Moderator's Comments:
Mod Comment Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks. Check your PMs for a guide.

Last edited by zaxxon; 09-14-2011 at 07:27 AM.. Reason: code tags, see PM
# 2  
Old 09-14-2011
sed is the main part in your script and it will only replace H with M.
Also, if there is only one file, you don't need the for loop.

Code:
awk '{print ++i" "$2}/H/{$0=$2;gsub(/H/,"M");print ++i" "$0}' inputFile

Script based...
Code:
#!/bin/bash

OLD="H"
NEW="M"

DPATH="/home/me/file.dat"
BPATH="/home/me/out.dat"
TFILE="/tmp/out.tmp.$$"

i=0
while read lineno string
do
  ((i=i+1))
  echo "$i $string" >> $BPATH
  newstring=$( echo $string | tr $OLD $NEW )
  if [ $string != $newstring ]; then
    ((i=i+1))
    echo "$i $newstring" >> $BPATH
  fi
done < $DPATH

--ahamed

Last edited by ahamed101; 09-14-2011 at 07:50 AM..
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 09-14-2011
alternative
Code:
awk '{print $2;if($2~/H/){gsub(/H/,"M",$2);print $2;}}' yourFile|nl -nln

This User Gave Thanks to sk1418 For This Post:
# 4  
Old 09-14-2011
Thank you both so much.
The loop was useless indeed.
You used some commands I didn't know yet, such as "while read". I just learnt it.
Now I have to implement this :
beside the column with the 100 words, there are other 12 columns with numbers(which can assume only the values of 0 or 1).
I have to find a way to read the 200x12 matrix of numbers and then, in the 10th column, I have to write 1 instead of 0 when the string was changed from H to M and keep the numbers (0 or 1) elsewhere.
For example:

Code:
1 HOT     0 1 0 0 0 0 0 0 0 0 0 0 
2 MOT     0 1 0 0 0 0 0 0 0 1 0 0
3 WITH   0 0 1 0 0 0 0 0 0 0 0 0 
4 WITM   0 0 1 0 0 0 0 0 0 1 0 0
..      ................................
100   ...............................

Does anybody know an easy way or just an idea to do that?
# 5  
Old 09-14-2011
@leaf
what have you tried?
the easy way is : understanding the solutions try to add "$10=1" on the "if" (or /H/ from ahamed) block. well you may need to do some changes on print to get right output.

I knew that the text I typed above would be longer than the codes. but please get hands dirty.
# 6  
Old 09-14-2011
@sk1418
Actually my idea was to read a bidimensional array from the file and then assign the values 0 or 1 according the criterion I wrote before. I'm stuck on the way to 2D arrays in bash scripting Smilie
# 7  
Old 09-14-2011
why not doing the s/H/M/ and 1/0 trick in one shot by awk?
e.g
if match "H", add a new line s/H/M/, then set the 1 on the certain column?
or I didn't understand your requirement correctly. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing strings

Hello All, I have two files with delimited | file 1 : 1|2|3 11|12|13 22|23|24 and file 2 : 1|4|5|6 11|14|15|16 22|25|26 I want to replace the value '1' in file 2 with the values in file 1 '1|2|3' so the final output will look like 1|2|3|4|5|6 11|12|13|14|15|16 22|23|24|25|26 (3 Replies)
Discussion started by: ArunKumarM
3 Replies

2. Shell Programming and Scripting

Replacing strings in various files

i'm trying to figure out the easiest way to replace a string: pineapple pineapple-reg basketball basketball-reg football foot-reg-ball i'm storing the above in a file called wordstoreplace.txt for each line above, the word in the first column is to be replaced by the word in the second... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. Shell Programming and Scripting

Copying lines between two strings

Hello All, I want to copy some lines from one file to other with following condition. Only lines between two specified strings should copy. Example :- "My First String " Some_Other_String .... Some_Other_String .... Some_Other_String .... "My Second String" So only... (5 Replies)
Discussion started by: anand.shah
5 Replies

4. Shell Programming and Scripting

Replacing strings

The code below gives the string "test1.txt" even though "tessdsdt" does not match "test1.txt". I would like to return "" if there is no match and return some kind of error that I can capture and decide what to do. echo test1.txt | awk -v src="tessdsdt" -v dst="test" '{sub(src,dst); print}' (16 Replies)
Discussion started by: kristinu
16 Replies

5. Shell Programming and Scripting

Copying of multiple columns of one table to another by mapping with particular strings.

Hi, I would like to copy some columns from a particular file by mapping with the string names. i am using the .csv file format. my one file consist of 100 of columns but i want only particular 4 columns such as ( First_name, Middle_name,Last_name & Stlc). but they are listed in many files... (15 Replies)
Discussion started by: dsh007
15 Replies

6. Shell Programming and Scripting

Replacing Strings in a File

I have a input file which looks like this: Value1="" Value2="" Value3="" ListOfValues=" $Value1 $Value2 $Value3" I have another program which computes the values ($val1, $val2, $val3). So if $val1 is 'A', $val2 is 'B' and $val3 is 'C', I should edit the input file so it will look like:... (6 Replies)
Discussion started by: laiko
6 Replies

7. Shell Programming and Scripting

Replacing Strings in a File

I have a input file which looks like this: Value1="" Value2="" Value3="" ListOfValues=" $Value1 $Value2 $Value3" I have another program which computes the values ($val1, $val2, $val3). So if $val1 is 'A', $val2 is 'B' and $val3 is 'C', I should edit the input file so it will look like:... (0 Replies)
Discussion started by: laiko
0 Replies

8. Shell Programming and Scripting

Replacing strings

I am trying to take the two line version of this: mv myFile.txt myFile.txt.bak sed 's/foo/bar/g' myFile.txt.bak > myFile.txt and make it into a shell script with three parameters. First two parameters are the string and string replacement and the third is file. So far this is what I have... (5 Replies)
Discussion started by: gordonheimer
5 Replies

9. Shell Programming and Scripting

Replacing strings in csv file.

Hi, I have a problem.. 1) I have a file that contains the lines as below : VRF-TM_DummyLab/mse02.lab,mse02.lab,ge-2/0/7.222 VRF-EMS_HUAWEI_MSAN_208/mse01.lab,mse01.lab,xe-1/0/0.208 2) I need a method to read this file, line by line from :... (5 Replies)
Discussion started by: msafwan82
5 Replies

10. Shell Programming and Scripting

copying strings...

hey i want to know the unix command for copying 1 string to another and allso adding a third string to the result. for eg: a b="nbno" c="uioio" i want to copy contents of b to a and the append the contents of c to the contents of a and the result shud be in string a (1 Reply)
Discussion started by: priya_9patil
1 Replies
Login or Register to Ask a Question