03-10-2010
Merge lines from one file if pattern matches
I have one comma separated file (a.txt) with two or more records all matching except for the last column.
I would like to merge all matching lines into one and consolidate the last column, separated by ":". Does anyone know of a way to do this easily?
I've searched the forum but most talked about two files or other types of merge...
Ex. I have this file a.txt:
Quote:
TEST1,000A,20,1,,_ABC_
TEST1,000A,20,1,,_DEF_
TEST1,000A,20,1,,_GHI_
TEST2,000B,80,,A,_GHI_
TEST2,000B,80,,A,_JKL_
I need output like so:
Quote:
TEST1,000A,20,1,,_ABC_:_DEF_:_GHI_
TEST2,000B,80,,A,_GHI_:_JKL_
Thank you.
Gianni
10 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
I have huge file. I want to copy the lines which have first character as 2 or 7, and also which has fist two characters as 90. I need only these records from file. How I can acheive this. Can somebody help me..... (2 Replies)
Discussion started by: cs_banda
2 Replies
2. Shell Programming and Scripting
Howdy.
I know this is most likely possible using sed or awk or grep, most likely a combination of them together, but how would one go about running a grep like command on a file where you only try to match your pattern to the second field in a line, space delimited?
Example:
You are... (3 Replies)
Discussion started by: LordJezoX
3 Replies
3. Shell Programming and Scripting
Hello,
I have searched forum trying to find a solution to my problem, but could not find anything or I did not understand the examples....
I should say, I am very inexperienced with text processing.
I have a text file with approx 60k lines in it.
I need to merge lines based on the number... (8 Replies)
Discussion started by: Bertik
8 Replies
4. Shell Programming and Scripting
Hi people...
I normally find with out any problem the solutions I need just by searching. But for this I'm not having any joy or jsut failing to adapt what I'ev found to work.
I have applciation report that doesn't allow for manipulation at creation so I want to do some post modifcation... (2 Replies)
Discussion started by: nhatch
2 Replies
5. Shell Programming and Scripting
I have a file like this. Pls help me to solve this .
(I should look for only Message : 111 and need to print the start time to end time
Need to ignore other type of messages. Ex: if first message is 111 and second message is 000 or anything else then ignore the 2nd one and print start time of the... (1 Reply)
Discussion started by: mnjx
1 Replies
6. Shell Programming and Scripting
Hi,
I have requirement that I need to split my input file into two files based on a search pattern "abc"
For eg. my input file has below content
abc
defgh
zyx
I need file 1 with
abc
and file2 with
defgh
zyx
I can use grep command to acheive this. But with grep I need... (8 Replies)
Discussion started by: sbhuvana20
8 Replies
7. Shell Programming and Scripting
File
6 dbnawldb010-b office Memphis_Corp_SQL_Diff Memphis-Corp-SQL-Inc-Application-Backup 03/09/11 03:24:04
42 luigi-b IPNRemitDB Memphis_Corp_SQL_Diff Memphis-Corp-SQL-Inc-Application-Backup 03/10/11 00:41:36
6 ebs-sqldev1-b IPNTracking Memphis_Corp_SQL_Diff... (4 Replies)
Discussion started by: ajiwww
4 Replies
8. UNIX for Dummies Questions & Answers
Hello! i have a text file.. which contains the data as follows
i want to merge the declarations lines pertaining to one datatype in to a single line as follows
i've searched the forum for help.. but couldn't find much help.. how can i do this?? (1 Reply)
Discussion started by: a_ba
1 Replies
9. Shell Programming and Scripting
Hi,
I have a requirement to merge multiple lines based on search pattern. The search criteria is : it will search for CONSTRAINT and when it found CONSTRAINT, it will merge all lines to 1 line till it founds blank line.
For Example:
CREATE TABLE "AMS_DISTRIBUTOR_XREF"
(
"SOURCE"... (5 Replies)
Discussion started by: satyaatcgi
5 Replies
10. UNIX for Beginners Questions & Answers
I have a text file with many thousands of lines, a small sample of which looks like this:
InputFile:PS002,003 D -1 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 6 6 -1 -1 -1 -1 0 509 0
PS002,003 PSQ 0 1 7 18 1 0 -1 1 1 3 -1 -1 ... (5 Replies)
Discussion started by: jvoot
5 Replies
LEARN ABOUT REDHAT
filterdiff
FILTERDIFF(1) FILTERDIFF(1)
NAME
filterdiff - extract or exclude diffs from a diff file
SYNOPSIS
filterdiff [-i PATTERN] [-p n] [--strip=n] [--addprefix=PREFIX]
[-x PATTERN] [--verbose] [-v] [-z] [-# RANGE | --hunks=RANGE]
[--lines=RANGE] [--annotate] [--format=FORMAT] [--as-numbered-lines=WHEN]
[file...]
filterdiff {--help | --version | --list | --grep ...}
DESCRIPTION
You can use filterdiff to obtain a patch that applies to files matching the shell wildcard PATTERN from a larger collection of patches. For
example, to see the patches in patch-2.4.3.gz that apply to all files called lp.c:
filterdiff -z -i '*/lp.c' patch-2.4.3.gz
If neither -i nor -x options are given, -i '*' is assumed. To remove lines from a file that are not part of a patch, you might do this:
filterdiff message > patch
Note that the interpretation of the shell wildcard pattern does not count slash characters or periods as special (in other words, no flags
are given to fnmatch). This is so that ``*/basename''-type patterns can be given without limiting the number of pathname components.
You can use both unified and context format diffs with this program.
OPTIONS
-i PATTERN
Include only files matching PATTERN. All other lines in the input are suppressed.
-x PATTERN
Exclude files matching PATTERN. All other lines in the input are displayed.
-p n When matching, ignore the first n components of the pathname.
-# RANGE | --hunks=RANGE
Only include hunks within the specified RANGE. Hunks are numbered from 1, and the range is a comma-separated list of numbers or
``first-last'' spans; either the first or the last in the span may be omitted to indicate no limit in that direction.
--lines=RANGE
Only include hunks that contain lines from the original file that lie within the specified RANGE. Lines are numbered from 1, and the
range is a comma-separated list of numbers or ``first-last'' spans; either the first or the last in the span may be omitted to indi-
cate no limit in that direction.
--annotate
Annotate each hunk with the filename and hunk number.
--format=unified|context
Use specified output format.
--strip=n
Remove the first n components of pathnames in the output.
--addprefix=PREFIX
Prefix pathnames in the output by PREFIX.
--as-numbered-lines=before|after
Instead of a patch fragment, display the lines of the selected hunks with the line number of the file before (or after) the patch is
applied, followed by a TAB character and a colon, at the beginning of each line. Each hunk except the first will have a line con-
sisting of ``...'' before it.
-v, --verbose
Always show non-diff lines in the output. By default, non-diff lines are only shown when excluding a filename pattern.
-z Decompress files with extensions .gz and .bz2.
--help Display a short usage message.
--version
Display the version number of filterdiff.
--list Behave like lsdiff(1) instead.
--grep Behave like grepdiff(1) instead.
EXAMPLES
To see all patch hunks that affect the first five lines of a C file:
filterdiff -i '*.c' --lines=-5 < patch
To see the first hunk of each file patch, use:
filterdiff -#1 patchfile
To see patches modifying a ChangeLog file in a subdirectory, use:
filterdiff -p1 Changelog
To see the complete patches for each patch that modifies line 1 of the original file, use:
filterdiff --lines=1 patchfile | lsdiff |
xargs -rn1 filterdiff patchfile -i
To see all but the first hunk of a particular patch, you might use:
filterdiff -p1 -i file.c -#2- foo-patch
If you have a very specific list of hunks in a patch that you want to see, list them:
filterdiff -#1,2,5-8,10,12,27-
To see the lines of the files that would be patched as they will appear after the patch is applied, use:
filterdiff --as-numbered-lines=after patch.file
You can see the same context before the patch is applied with:
filterdiff --as-numbered-lines=before
patch.file
Filterdiff can also be used to convert between unified and context format diffs:
filterdiff -v --format=unified context.diff
SEE ALSO
lsdiff(1), grepdiff(1)
AUTHOR
Tim Waugh <twaugh@redhat.com>.
patchutils 23 Aug 2002 FILTERDIFF(1)