Scripting - replace a line <SOLVED>

Using applications, configuring, problems
Post Reply
Message
Author
dcung
Posts: 242
Joined: Sat 14 Jul 2018, 00:11
Location: Down Under - 3rd rock from Sun.

Scripting - replace a line <SOLVED>

#1 Post by dcung »

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... :)

Thank you.
Last edited by dcung on Sat 18 Aug 2018, 07:19, edited 1 time in total.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#2 Post by musher0 »

Hello dcung.

Your choice of approaches!
https://www.ask.com/web?l=dir&qsrc=0&o= ... +new+lines

IHTH
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#3 Post by fredx181 »

Hello dcung,

This should do from terminal in that directory:

Code: Select all

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: Select all

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: Select all

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

dcung
Posts: 242
Joined: Sat 14 Jul 2018, 00:11
Location: Down Under - 3rd rock from Sun.

#4 Post by dcung »

fredx181 wrote: Or set "word1-word2-word3" as variable first:

Code: Select all

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... :lol: :lol: :lol:
Thanks Fred.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#5 Post by musher0 »

Hi dcung.

May I suggest: (please read the notes and comments)

Code: Select all

#!/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
[~/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
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

Post Reply