[NTLUG:Discuss] change words on every line in a file

Fred James fredjame at fredjame.cnc.net
Mon Mar 26 11:31:36 CDT 2012


o k wrote:
> All
> Sorry I repost for clarify Is there any have a good suggestion on change words on every line in a file, line by line. 
> ex:
> original: 
> dog   1234cat   1234mouse 1234horse 1234
> after changed: dog 9899cat 9899mouse 9899house 9899
>
> I have this, but seems to not working: 
> #!/bin/shv='9899'cat file | while read s1 s2doif [ "$s2" != "$v" ]; then    echo $s1 '--' $s2   cat  "$s1" ' ' "$v" >> file //add new line to file?  sed -i '/$s2/ d' file       //delete a line contain value $2?fidoneexit 0
> thank for your help.
>
>
>
>   
>> From: aaasssxxx at hotmail.com
>> To: discuss at ntlug.org
>> Date: Mon, 26 Mar 2012 14:40:42 +0000
>> Subject: Re: [NTLUG:Discuss] change words on every line in a file
>>
>>
>> Fred and all,
>> Sorry for confusion, the context (file) will be:
>> These is only 2 columns per line. space between 2 values can be change to desired space. (one space, 2 spaces... or tab)the first column is all different animal names.the second will be all same string. and I want to change them one line by one line through loop.$s2 value (was 1234)  should be replaced to $v (9899) -> after changed, second: v='9899', s2='9899'
>> Still looking for help.  Thanks.
>>
>>
>>     
>>> Date: Fri, 23 Mar 2012 19:03:00 -0500
>>> From: fredjame at fredjame.cnc.net
>>> To: discuss at ntlug.org
>>> Subject: Re: [NTLUG:Discuss] change words on every line in a file
>>>
>>> o k wrote:
>>>       
>>>> All is there any have a good suggestion on change words on every line in a file. ex:original: dog     1234cat     1234mouse  1234houesr 1234 after changed dog 9899cat 9899mouse 9899house 9899 I have this, but seems to not woking: #!/bin/sh
>>>> v='9899'
>>>> cat file | while read s1 s2
>>>> do
>>>> if [ "$s2" != "$v" ]; then
>>>>     echo $s1 '--' $s2
>>>>    cat  "$s1" ' ' "$IPADDR" >> file
>>>>    sed -i '/$s2/ d' file
>>>> fi
>>>> done
>>>> exit 0 thank for your help.   		 	   		  
>>>>   
>>>>         
>>> Context is all important ... the read (within the while statement) 
>>> suggest that there are 2 and only 2 values per line (i.e., each 
>>> line/record contains two space separated words ...
>>>     first:  is that true?
>>>     second: v='9899', s2=? (dog, cat, 1234dog, 1234cat, etc)? ... all of 
>>> these will be unequal to 9899 ... is that what you want?
>>> ... that all my commends/questions for the moment
>>> Regards
>>> Fred James
>>>       
Someone else (responding to the digest form) suggested SED ... SED does 
the comparison/substitution you are seeking, and so does the 'ex' 
command line within 'vi'.
For example:  the sed command given by the other responder ...
    sed s/"1234"/"9899"/g < /tmp/filename.tmp> /path/to/filename

The "< /tmp/filename.tmp> /path/to/filename" part redirects input from a 
file, and then output to another file
The sed command itself: "sed s/"1234"/"9899"/g"
    s means search
    "1234" is the pattern to search for, but I don't think the "" is 
required in your case
    "9899" is the pattern to replace with ... again ... but I don't 
think the "" is required in your case
    g mean global within the line ... that means replace every 
occurrence in the line.

Done with 'ex' it would be ...
:%s/1234/9899/g
... the difference here being
    : opens up the 'ex' command line
    % is the address range (which lines to search) ... % is a special 
case address that means "all"
    everything else is the same.

Hope that helps
Regards
Fred James



More information about the Discuss mailing list