Text file manipulations

Hello All,

I have three txt files

***main.txt*****

code test
line code test3
asdfasdf
do for while

line1: 

code test
line code test3
asdfasdf
do for while

line2:

code test
line code test3
asdfasdf
do for while

*****end******
--------------------------------------------
*****path1***

datapath1
datapath2
datapath3

***end*****
-----------------------------------------------
****path2****

pathdata1
pathdata2
pathdata3

***end******

I would like to generate 3 main.txt files with the line1 and line2 replaced with corresponding lines from path1 and path2 files.

example
main_1.txt

code test
line code test3
asdfasdf
do for while

datapath1

code test
line code test3
asdfasdf
do for while

pathdata1

code test
line code test3
asdfasdf
do for while

*****end******

Any help would be appreciated.

Thanks,
Avatar

And what code have you tried so far...?

Hi Jim,
Here is what I have tried but need to be able to give variables as inputs to the echo statements and read those variables from the txt files.

#!/bin/bash

i=1
while read line;do
  if((i==7));then
    echo 'dir1='
  elif((i==48));then
    echo 'dir2='
  else
    echo "$line"
  fi
  ((i++))
done  < main.txt > main1.txt

Thanks,
Avatar

0 $ cat main.txt 
line
line0:
bl�dsinn

line1:

namal bl�d
line2:

schluss
0 $ cat paths.txt 
/some/path/1
/some/other/path/2
/some/path/3

0 $ bash txt_manip.sh 
0 $ cat main.txt
line
dir0=/some/path/1
bl�dsinn

dir1=/some/other/path/2

namal bl�d
dir2=/some/path/3

schluss
0 $ 

With this one:

#!/bin/bash
FILE=main.txt
cp $FILE $FILE.bkup.$$
declare -a ar_PATH=($(<paths.txt))
declare -a ar_LINE=($(grep "line.:" "$FILE"))
C=0

for LINE in "${ar_LINE[@]}"
do
	sed s,"$LINE","dir$C=${ar_PATH[$C]}",g -i "$FILE" > "${FILE/\.txt/$C}.txt"
	((C++))
done

EDIT: Bold stuff has been added after the example, to match the each new filename.
Red part has been removed, to accomplish above said.

Hope this helps

1 Like

If I understood correctly what you specified in post #1 in this thread, you might might also want to try something more like:

awk '
FNR == 1 {
	# Note start of an input file...
	f++
}
{	# Count the number of lines in each input file and gather text.
	l[f, ++lc[f]] = $0
	next
}
END {	# Verify that the 2nd and 3rd files are not empty and contain the same
	# number of lines.
	if(lc[2] != lc[3] || lc[2] == 0) {
		print "2nd and 3rd files must contain the same # of lines > 0"
		exit 1
	}
	# Create one output file for each line in the 2nd and 3rd files.
	for(i = 1; i <= lc[2]; i++) {
		# Create the output file name.
		fn = "main_" i ".txt"
		# Copy lines from the appropriate input file to the output file.
		for(j = 1; j <= lc[1]; j++)
			print (l[1, j] ~ /^line1:/ ? l[2, i] : \
			       l[1, j] ~ /^line2:/ ? l[3, i] : \
			       l[1, j]) > fn
		# Close the output file.
		close(fn)
	}
}' main.txt path1 path2

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .

1 Like

As you explicitely stated 3 lines to be inserted, try also

awk ' 
FNR == 1        {f++
                }   
f < 3           {line[FNR, f]=$0
                 next
                }
                {TMP=substr ($1, 5, 1)
                 TML=/line[12]/
                 for (i=1; i<=3; i++)   {if (TML) $0 = line[i, TMP]
                                         print > "main_" i ".txt"  
                                        }
                }

'  path1 path2 main.txt
1 Like

Thank you so much!

Beautifully explained too!!