Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Fri 14 Dec 2018, 05:35
All times are UTC - 4
 Forum index » House Training » Users ( For the regulars )
Scripting - replace a line <SOLVED>
Moderators: Flash, Ian, JohnMurga
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [5 Posts]  
Author Message
dcung

Joined: 13 Jul 2018
Posts: 230
Location: Down Under - 3rd rock from Sun.

PostPosted: Sat 18 Aug 2018, 00:44    Post subject:  Scripting - replace a line <SOLVED>
Subject description: Have hundred of files to do.
 

Hi,

I have 100 files in a directory.
I'd like to replace one line in each file with 2 new lines.

word1-word2-word3

with

word8-word9
word1-word2-word3 /word4/word5/word6/word7


If it was a line, my rusty memory remember that I should create a script that goes something like

for $i in *
do s/word1-word2-word3/word1-word2-word3 \/word4\/word5\/word6\/word7/

I don't mind doing it in two stages, i.e. substitute a line and later insert a line.

BTW - I use XenPup75 or Fatdog64.

Calling scripting gurus to help please... Smile

Thank you.

Last edited by dcung on Sat 18 Aug 2018, 03:19; edited 1 time in total
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 13162
Location: Gatineau (Qc), Canada

PostPosted: Sat 18 Aug 2018, 02:39    Post subject:  

Hello dcung.

Your choice of approaches!
https://www.ask.com/web?l=dir&qsrc=0&o=ffx&q=replace+one+line+in+each+file+with+2+new+lines

IHTH

_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
fredx181


Joined: 11 Dec 2013
Posts: 3577
Location: holland

PostPosted: Sat 18 Aug 2018, 02:49    Post subject:  

Hello dcung,

This should do from terminal in that directory:
Code:
for i in $(ls -A)
do
sed -i "s|word1-word2-word3|word8-word9\nword1-word2-word3 /word4/word5/word6/word7|g" "$i"
done

Or set "word1-word2-word3" as variable first:
Code:
VAR="word1-word2-word3"
for i in $(ls -A)
do
sed -i "s|$VAR|word8-word9\n$VAR /word4/word5/word6/word7|g" "$i"
done

Note that this will change your files because of the "-i" switch of sed.
Better first test with the -e switch on only a few files in a directory (and you'll just see the output in terminal, no files modified).
Code:
for i in $(ls -A)
do
sed -e "s|word1-word2-word3|word8-word9\nword1-word2-word3 /word4/word5/word6/word7|g" "$i"
done


Fred

_________________
Dog Linux website
Back to top
View user's profile Send private message 
dcung

Joined: 13 Jul 2018
Posts: 230
Location: Down Under - 3rd rock from Sun.

PostPosted: Sat 18 Aug 2018, 03:19    Post subject:  

fredx181 wrote:

Or set "word1-word2-word3" as variable first:
Code:
VAR="word1-word2-word3"
for i in $(ls -A)
do
sed -i "s|$VAR|word8-word9\n$VAR /word4/word5/word6/word7|g" "$i"
done



I used this one. Works like a charm!
Did it in a second, what would have taken me at least 3 days to do manually... Laughing Laughing Laughing
Thanks Fred.
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 13162
Location: Gatineau (Qc), Canada

PostPosted: Sat 18 Aug 2018, 03:49    Post subject:  

Hi dcung.

May I suggest: (please read the notes and comments)
Code:
#!/bin/bash
# /root/my-applications/bin/replace1linewith2.sh
#### # set -xe
B="word8-word9"
A="word1-word2-word3"
C="/word4/word5/word6/word7"

TxT=nonsense # or whatever the file name
rm -f mod-$TxT* # Clean previous tries.

N=1
cd /root/my-documents # or wherever the documnents are.
ls -1 $TxT* > texts.lst
while read text;do
   echo . > mod-$text-$N # Adding empty line for readability
   while read line;do
      if [ "$line" = "$A" ];then
         echo -e "$B\n$A $C" >> mod-$text-$N
      else
         echo $line >> mod-$text-$N
      fi      
   done < $text   
done < texts.lst
# N="`expr $N + 1`" # Could be used for versioning.
# set +xe
# more /root/my-documents/mod-* # Optional viewing of results
exit

~~~~~~~~~~~~~~~~~~~~~~~
NOTES
-- for dcung, PuppyLinux Forum, Sat. Aug. 18, 2018.
-- Make sure the original text ends with an empty line.
Otherwise, any script like this will skip the last line of text.
         
In this test, the following three files were used as starting points:
-- nonsense3.txt
folbolo folbolo
blobloblo
toto
word1-word2-word3
strum strum strum
bebop hiphop

-- nonsense2.txt
folbolo folbolo
blobloblo
tah tah
word1-word2-word3
strum strum strum
bebop hiphop

-- nonsense.txt
falbala falbala
blablabla
toto
word1-word2-word3
strum strum strum
bebop hiphop

Quote:
[~/my-documents]>more mod-non*
.
folbolo folbolo
blobloblo
toto
word8-word9
word1-word2-word3 /word4/word5/word6/word7
strum strum strum
bebop hiphop
.
folbolo folbolo
blobloblo
tah tah
word8-word9
word1-word2-word3 /word4/word5/word6/word7
strum strum strum
bebop hiphop
.
falbala falbala
blablabla
toto
word8-word9
word1-word2-word3 /word4/word5/word6/word7
strum strum strum
bebop hiphop


IHTH

_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [5 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » House Training » Users ( For the regulars )
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0862s ][ Queries: 12 (0.0111s) ][ GZIP on ]