Concatenation of two patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenation of two patterns
# 1  
Old 12-06-2012
Concatenation of two patterns

Read the lines from the file[file2.txt] and concatenate the lines into single line then
search the string "INPUT=" from the another file[file1.txt] and if "INPUT" is present in that file[file1.txt]
assign the concatenate patterns into "INPUT=" pattern.

example:

file1.txt
Code:
<some lines>
INPUT = 
<some lines>

file2.txt
Code:
../A/B/sample.c
../A/c/sample1.c
../e/f/sample2.c
../d/B/sample3.c

Output:

INPUT= ../A/B/sample.c ../A/c/sample1.c ../e/f/sample2.c ../d/B/sample3.c

Last edited by joeyg; 12-06-2012 at 11:04 AM.. Reason: Please wrap data and sripts with CodeTags
# 2  
Old 12-06-2012
Code:
str=`cat file2.txt | tr '\n' ' '`
sed 's/INPUT=/INPUT= \$str/g' file1.txt

# 3  
Old 12-06-2012
try also:
Code:
awk -v f2=file2.txt '/INPUT *=/ {printf $0 ; while (getline x < f2) printf " "x ; } 1' file1.txt

# 4  
Old 12-06-2012
Quote:
Originally Posted by krsureshmca
Read the lines from the file[file2.txt] and concatenate the lines into single line then
search the string "INPUT=" from the another file[file1.txt] and if "INPUT" is present in that file[file1.txt]
assign the concatenate patterns into "INPUT=" pattern.

example:

file1.txt
Code:
<some lines>
INPUT = 
<some lines>

file2.txt
Code:
../A/B/sample.c
../A/c/sample1.c
../e/f/sample2.c
../d/B/sample3.c

Output:

INPUT= ../A/B/sample.c ../A/c/sample1.c ../e/f/sample2.c ../d/B/sample3.c
I note that you are looking for INPUT= (without a space) in file1.txt and the Output specified INPUT= (without a space), but your sample file1.txt has INPUT = (with a space before and after the equals sign).

Since there aren't any code tags where you show us the Output you want (even after joeyg's edits), it is hard to tell if you're looking for spaces or tabs as separators in the Output line. (From looking at the quoted text above it looks like you have single spaces between lines from file2.txt but several (seven) spaces before the first line's contents.

Due to using single quotes instead of double quotes, I don't think PikK45's proposal is going to do what is wanted and the use of:
Code:
str=`cat file2.txt | tr '\n' ' '`

is more efficiently written as:
Code:
str=`tr '\n' ' ' < file2.txt`

or, preferably as:
Code:
str=$(tr '\n' ' ' < file2.txt)

Due to a small logic error, rdrtx1's proposal will output an additional INPUT= (along with any number of spaces found on the input line before and after the = at the end of the line. And, if there is more than one INPUT= (including zero or more spaces before the =), only the first line will contain the data accumulated from reading file2.txt. It also makes an unnecessary assumption that a filename found in file2.txt will never contain a percent character (which could also mess up the output produced).

Your statements above require that file2.txt be read before processing file1.txt, but I agree with rdrtx1's proposal in assuming that there is no need to look at file2.txt at all if file1.txt doesn't contain an INPUT= line.

I think the following comes closer to matching your requirements (note that /usr/bin/awk on Solaris systems doesn't support the -v option; so on Solaris systems use /usr/xpg4/bin/awk or nawk instead of just awk):
Code:
awk -v f2=file2.txt '
/INPUT *=/ {
        if(f2c == "")
                while(getline f < f2)
                        f2c = f2c " " f
        printf("%s%s\n", $0, f2c)
        next
}
{       print}' file1.txt

When run in a directory with file2.txt containing the text you listed above for file2.txt and file1.txt containing:
Code:
<some lines>
INPUT = 
<some more lines>
INPUT=  
<and some final lines>

(note that the 2nd line in file1.txt has a single space both before and after the equals sign like you had listed above for file1.txt, but the 4th line does not contain any spaces), the awk script above produces the following output:
Code:
<some lines>
INPUT =  ../A/B/sample.c ../A/c/sample1.c ../e/f/sample2.c ../d/B/sample3.c
<some more lines>
INPUT= ../A/B/sample.c ../A/c/sample1.c ../e/f/sample2.c ../d/B/sample3.c
<and some final lines>

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - Find files excluding file patterns and subfolder patterns

Hello. For a given folder, I want to select any files find $PATH1 -f \( -name "*" but omit any files like pattern name ! -iname "*.jpg" ! -iname "*.xsession*" ..... \) and also omit any subfolder like pattern name -type d \( -name "/etc/gconf/gconf.*" -o -name "*cache*" -o -name "*Cache*" -o... (2 Replies)
Discussion started by: jcdole
2 Replies

2. Shell Programming and Scripting

Concatenation of files with same naming patterns dynamically

Since my last threads were closed on account of spamming, keeping just this one opened! Hi, I have the following reports that get generated every 1 hour and this is my requirement: 1. 5 reports get generated every hour with the names "Report.Dddmmyy.Thhmiss.CTLR"... (5 Replies)
Discussion started by: Jesshelle David
5 Replies

3. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

4. Shell Programming and Scripting

Help with String concatenation

I have a script which is migrated from AIX to Linux & now while running it is no able to concatenate string values The string concatenation step under while loop is not displaying desired result Please find below the piece of code: while read EXT_FILE ; do EXT_FILE=$EXT_FILE.ext.sent echo... (7 Replies)
Discussion started by: PreetArul
7 Replies

5. Shell Programming and Scripting

Line concatenation help

please help me on this.... cat /xx.txt 2:1 2 2:2 24 8:0 0 9:0 0 Expected result would be 2:1 2 2:2 24 8:0 0 9:0 0 (4 Replies)
Discussion started by: Aditya.Gurgaon
4 Replies

6. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

7. Shell Programming and Scripting

Concatenation

How can I do this: date = 4 -----------> 04 Month= 3-----------> 03 I wish to make a varibale named Var1 which will hold the value of current date and month value as: 2 digits for date. 2 digits for month. So finally var1 should hold value as 0403 --- MMDD (11 Replies)
Discussion started by: Asteroid
11 Replies

8. UNIX for Dummies Questions & Answers

File Concatenation

Hi, I want to write a generic shell script, which can concatenate n number of files passed as parameter ;to an output file which is again a parameter Example catfl.sh outfl.txt a.txt b.txt c.txt (3 files to be concatenated into a file outfl.txt) catfl.sh outfl.txt a.txt b.txt(2 files to be... (3 Replies)
Discussion started by: samit_9999
3 Replies

9. UNIX for Dummies Questions & Answers

string concatenation

my input file contains thousands of lines like below 234A dept of education 9788 dept of commerce 8677 dept of engineering How do i add a delimeter ':' after FIRST 4 CHARACTERS in a line 234A:dept of education 9788:dept of commerce 8677:dept of engineering (7 Replies)
Discussion started by: systemsb
7 Replies

10. Shell Programming and Scripting

Concatenation

What is syntax for String concatenation? I have $1 as directory. $var is some variable value '/' String value. How do I have to concatenate if I have to run utility - util $1 followed by '/' followed by $var There is no space between these three. (2 Replies)
Discussion started by: videsh77
2 Replies
Login or Register to Ask a Question