Text file manipulations


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Text file manipulations
# 1  
Old 09-24-2015
Text file manipulations

Hello All,

I have three txt files

***main.txt*****
Code:
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***
Code:
datapath1
datapath2
datapath3

***end*****
-----------------------------------------------
****path2****
Code:
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:
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
Moderator's Comments:
Mod Comment Please use CODE tags for sample input and output as well as for code segments.

Last edited by Don Cragun; 09-24-2015 at 03:43 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 09-24-2015
And what code have you tried so far...?
# 3  
Old 09-24-2015
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.

Code:
#!/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
# 4  
Old 09-24-2015
Code:
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:
Code:
#!/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
This User Gave Thanks to sea For This Post:
# 5  
Old 09-24-2015
If I understood correctly what you specified in post #1 in this thread, you might might also want to try something more like:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 09-25-2015
As you explicitely stated 3 lines to be inserted, try also
Code:
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

This User Gave Thanks to RudiC For This Post:
# 7  
Old 09-28-2015
Thank you so much!

Beautifully explained too!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

3. UNIX for Dummies Questions & Answers

Help with Multiple Text Manipulations

Hey guys, I have a file which contains 22,373 ping trace outputs which I generated using a script I made, see excerpt below: ... PING6(72=40+8+24 bytes) 2001:630:301:1453:219:e3ff:fee7:8c2a --> 2406:8000:101:c020::24 32 bytes from 2406:8000:101:c020::24, icmp_seq=0 hlim=44 time=279.163 ms... (6 Replies)
Discussion started by: churchill
6 Replies

4. Shell Programming and Scripting

Some manipulations with files and folders. (loop, find, create and remove)

Hello! I need to realize such task. 1. In my user's home dir I have folder1; 2. In folder1 I have some (various count) subfolders with random names; 3. In these subfolders I have one file anyname.pdf (various name in each subfolder) and file content.txt (constant name in each subfolder) ##... (7 Replies)
Discussion started by: optik77
7 Replies

5. Shell Programming and Scripting

Time and Date Manipulations

Hi Guys... I do have a script that I need to use time or time function in my condition. The logic will be like if the current time of execution is between 7am and 7pm then do 1,2,3,etc else do 4,5,6. I need help is that function or how best can I do this. Thanks in advance. Please... (3 Replies)
Discussion started by: Phuti
3 Replies

6. Shell Programming and Scripting

File Manipulations

Hi All, I have a pipe delimited file with around 30 fields. What is the simple way to Update a value for any column. For ex. If i want to update 22 field with "album". Similarly, how to do this for the whole file and selective records of the file. Example file contents: ... (5 Replies)
Discussion started by: Joe2226
5 Replies

7. Shell Programming and Scripting

How to do String manipulations using Substring function in Shell

Hi, I have a scenario to just plug out the file name from the following location path. /opt/project/data/int/holdFiles/csv195687.csv So, how do I get just file name which is "csv195687.csv" from the above line using awk/shell scripting? Can we use indexOf and Substring in awk to get... (7 Replies)
Discussion started by: anilvvnn
7 Replies

8. UNIX for Advanced & Expert Users

Date manipulations

hi i am having a script in which i am supposed to extract data for three different dates...first date is current date second date is 15 days back third date is 40 days back for eg consider todays date 26062006 as first date then second date is 11062006 third date is 17052006 now in my... (2 Replies)
Discussion started by: rochitsharma
2 Replies

9. Shell Programming and Scripting

Time Manipulations

Hi All :D I have a long file having different fields like :- hh:mm:ss seconds 14:15:56 120 14:18:36 12 15:12:36 1500 I want to subtract the hh:mm:ss in line(2) from hh:mm:ss in line(1) & compare the output of substraction (obtained in... (10 Replies)
Discussion started by: vanand420
10 Replies

10. Shell Programming and Scripting

date manipulations

i need a date manipulation unix shell script, to find the difference between any two dates. kindly help me:confused: (2 Replies)
Discussion started by: user1
2 Replies
Login or Register to Ask a Question