Sponsored Content
Top Forums UNIX for Dummies Questions & Answers How to append portion of a file content to another file when a certain pattern is matching? Post 302820597 by bananamen on Thursday 13th of June 2013 03:51:13 AM
Old 06-13-2013
text1.txt
Code:
XXX XXX XXXX:1234,1111_111,ABCD 123456,XXXX XXXXXX:12345,XX,YYYYY1,ZZZZZ1,
XXX XXX XXXX:1234,1111_111,BCDF 123456,XXXX XXXXXX:12345,XX,YYYYY2,ZZZZZ2,
XXX XXX XXXX:1234,1111_111,CDEF 123456,XXXX XXXXXX:12345,XX,YYYYY3,ZZZZZ3,

text2.txt
Code:
ABCD 123456,1111,11111,111111,XX,AAAAAA1,BBBBBB1
CDEF 123456,1111,11111,111111,XX,AAAAAA2,BBBBBB2
BCDF 123456,1111,11111,111111,XX,AAAAAA3,BBBBBB3

Output:
Code:
XXX XXX XXXX:1234,1111_111,ABCD 123456,XXXX XXXXXX:12345,XX,AAAAAA1,BBBBBB1
XXX XXX XXXX:1234,1111_111,BCDF 123456,XXXX XXXXXX:12345,XX,AAAAAA2,BBBBBB3
XXX XXX XXXX:1234,1111_111,CDEF 123456,XXXX XXXXXX:12345,XX,AAAAAA3,BBBBBB2

With some edit on the code above i still cant get the result i wanted. Any one know how do i solve this ? Thank you.

Last edited by Scott; 06-13-2013 at 05:47 AM.. Reason: Code tags
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

if file is NOT empty, then append content to file

hi people, i have texts down.txt and down-new.txt and i want to check; - if down-new.txt is NOT empty, then write date and its content to /home/gc_sw/down.txt for example; down.txt:AAAA SSSS down-new.txt:123 456 and after checking down-new.txt is NOT empty, down.txt should... (10 Replies)
Discussion started by: gc_sw
10 Replies

2. Shell Programming and Scripting

awk - writing matching pattern to a new file and deleting it from the current file

Hello , I have comma delimited file with over 20 fileds that i need to do some validations on. I have to check if certain fields are null and then write the line containing the null field into a new file and then delete the line from the current file. Can someone tell me how i could go... (2 Replies)
Discussion started by: goddevil
2 Replies

3. Shell Programming and Scripting

Need to append matching strings in a file

Hi , I am writing a shell script to check pvsizes in linux box. # for i in `cat vgs1` > do > echo "########### $i ###########" > pvs|grep -i $i|awk '{print $2,$1,$5}'>pvs_$i > pvs|grep -i $i|awk '{print $1}'|while read a > do > fdisk -l $a|head -2|tail -1|awk '{print $2,$3}'>pvs_$i1 >... (3 Replies)
Discussion started by: nanduri
3 Replies

4. Shell Programming and Scripting

Help with ksh-to read ip file & append lines to another file based on pattern match

Hi, I need help with this- input.txt : L B white X Y white A B brown M Y black Read this input file and if 3rd column is "white", then add specific lines to another file insert.txt. If 3rd column is brown, add different set of lines to insert.txt, and so on. For example, the given... (6 Replies)
Discussion started by: prashob123
6 Replies

5. Shell Programming and Scripting

Append timestamp create .trg file for all content of an unzipped archive

Hi, I have a test.zip archive that contains test.zip --> (file_1.txt, file_2.txt , file_3.txt) I need to unzip the file like this, file_1_timestamp.txt file_1_timestamp.trg file_2_timestamp.txt file_2_timestamp.trg file_3_timestamp.txt file_3_timestamp.trg Could you please let me know... (7 Replies)
Discussion started by: Shandel
7 Replies

6. Shell Programming and Scripting

How to remove exisiting file content from a file and have to append new file content?

hi all, i had the below script x=`cat input.txt |wc -1` awk 'NR>1 && NR<'$x' ' input.txt > output.txt by using above script i am able to remove the head and tail part from the input file and able to append the output to the output.txt but if i run it for second time the output is... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

7. Shell Programming and Scripting

Insert content of a file into another file before given pattern

I need to insert file x2 into x1 right before first BBB line. $ cat x1 AAA 1 AAA 2 AAA 3 BBB 1 BBB 2 BBB 3 $ cat x2 XXX - insert 1 XXX - insert 2 I need to get AAA 1 AAA 2 AAA 3 XXX - insert 1 XXX - insert 2 BBB 1 (2 Replies)
Discussion started by: migurus
2 Replies

8. Shell Programming and Scripting

Big pattern file matching within another pattern file in awk or shell

Hi I need to do a patten match between files . I am new to shell scripting and have come up with this so far. It take 50 seconds to process files of 2mb size . I need to tune this code as file size will be around 50mb and need to save time. Main issue is that I need to search the pattern from... (2 Replies)
Discussion started by: nitin_daharwal
2 Replies

9. Shell Programming and Scripting

Insert content of file before the first occurrence of a line starts with a pattern in another file

Hi all, I'm new to scripting.. facing some problems while inserting content of a file into another file... I want to insert content of a file (file2) into file1, before first occurrence of "line starts with pattern" in file1 file1 ====== working on linux its unix world working on... (14 Replies)
Discussion started by: Jagadeesh Kumar
14 Replies

10. UNIX for Beginners Questions & Answers

awk to update file with partial matching line in another file and append text

In the awk below I am trying to cp and paste each matching line in f2 to $3 in f1 if $2 of f1 is in the line in f2 somewhere. There will always be a match (usually more then 1) and my actual data is much larger (several hundreds of lines) in both f1 and f2. When the line in f2 is pasted to $3 in... (4 Replies)
Discussion started by: cmccabe
4 Replies
GLOB(3) 								 1								   GLOB(3)

glob - Find pathnames matching a pattern

SYNOPSIS
array glob (string $pattern, [int $flags]) DESCRIPTION
The glob(3) function searches for all the pathnames matching $pattern according to the rules used by the libc glob() function, which is similar to the rules used by common shells. PARAMETERS
o $pattern - The pattern. No tilde expansion or parameter substitution is done. o $flags - Valid flags: o GLOB_MARK - Adds a slash to each directory returned o GLOB_NOSORT - Return files as they appear in the directory (no sorting). When this flag is not used, the pathnames are sorted alphabetically o GLOB_NOCHECK - Return the search pattern if no files matching it were found o GLOB_NOESCAPE - Backslashes do not quote metacharacters o GLOB_BRACE - Expands {a,b,c} to match 'a', 'b', or 'c' o GLOB_ONLYDIR - Return only directory entries which match the pattern o GLOB_ERR - Stop on read errors (like unreadable directories), by default errors are ignored. RETURN VALUES
Returns an array containing the matched files/directories, an empty array if no file matched or FALSE on error. Note On some systems it is impossible to distinguish between empty match and an error. CHANGELOG
+--------+--------------------+ |Version | | | | | | | Description | | | | +--------+--------------------+ | 5.1.0 | | | | | | | | | | GLOB_ERR was added | | | | +--------+--------------------+ EXAMPLES
Example #1 Convenient way how glob(3) can replace opendir(3) and friends. <?php foreach (glob("*.txt") as $filename) { echo "$filename size " . filesize($filename) . " "; } ?> The above example will output something similar to: funclist.txt size 44686 funcsummary.txt size 267625 quickref.txt size 137820 NOTES
Note This function will not work on remote files as the file to be examined must be accessible via the server's filesystem. Note This function isn't available on some systems (e.g. old Sun OS). Note The GLOB_BRACE flag is not available on some non GNU systems, like Solaris. SEE ALSO
opendir(3), readdir(3), closedir(3), fnmatch(3). PHP Documentation Group GLOB(3)
All times are GMT -4. The time now is 12:19 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy