Sponsored Content
Top Forums Shell Programming and Scripting Returning specific columns upon matching Post 302917845 by Don Cragun on Thursday 18th of September 2014 09:32:10 PM
Old 09-18-2014
Quote:
Originally Posted by vamsikrishna928
Thanks for the reply..

Unfortunately, this script is not giving results. The output file is empty. Here is the script I use. Please correct me if there is anything wrong.

Code:
dos2unix fileA
dos2unix fileB
awk 'NR==FNR{A[$1]; next} {for(i=1; i<=NF-5; i++) if($i in A) print $i, $(i+1), $(i+4), $(i+5)}' fileA FS=\| OFS=\| fileB > FileC

I need to get the result set to the new file FileC. Also, if the delimiter in fileB is comma instead of pipeline, where should I need to change..?

Thanks in advance.
Is the text marked in red above your way of saying: "I'm sorry I gave you bad information about my input file format; the field separator is a comma instead of a vertical bar. Can you please help me fix the code you gave me because I made a mistake?"

If that is what you intended to say, you could try something like this instead:
Code:
awk '
{       gsub(/\r/, "")
}
NR==FNR{A[$1]
        next
}
{       for(i=1; i<=NF-5; i++) 
                if($i in A)
                        print $i, $(i+1), $(i+4), $(i+5)
}' fileA FS=',' OFS=',' fileB > FileC

Note that the call to gsub() takes care of the carriage return removal from both of your input files so you no longer need to invoke dos2unix twice.
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Returning filename and matching lines

What I'm trying to do is to search through a list of files, and output the filename, followed by the lines that matched the pattern. I'm matching the string "letters.moreletters" in any one of searched files, and the output I'm trying to get is: program_1.txt 10 dsdsd sdsd dsd... (2 Replies)
Discussion started by: smb_uk
2 Replies

2. UNIX for Dummies Questions & Answers

Searching partial columns and returning maximum as output

Hello, I am just getting starting with awk and wondering if anyone could help with the following problem. I have a large file of data, 50,000 rows x 6 columns. I would like to search in blocks of 500 rows for a maximum value in a specific column and compile an output file that prints the... (0 Replies)
Discussion started by: xb_analysis
0 Replies

3. Shell Programming and Scripting

matching columns from two files

Hey, I have two files that have exactly the same format. They are both tab-delimited and contain 12 columns. However the # of rows vary. What I want to do is match columns # 5,6 and 7 between the two files. If they do match exactly (based on numbers) then I want the whole row from file 2 to... (1 Reply)
Discussion started by: phil_heath
1 Replies

4. UNIX for Dummies Questions & Answers

Matching corresponding columns in two different files

Hi to all, I have two separated files: FILE1 "V1" "V2" "V3" Mary James Nicole Robert Francisco Sophie Nancy Antony Matt Josephine Louise Rose Mark Simon Charles FILE2 "V1" "V2" "V3"... (2 Replies)
Discussion started by: eleonoral
2 Replies

5. UNIX for Dummies Questions & Answers

matching columns

Hello experts, I have this problem, I need to match values based on two files, this is what I have: file1 1.1 1.2 1.3 5.5 1.4 1.5 1.6 file2 1 a 2 B 3 C 4 D 5 z (7 Replies)
Discussion started by: Gery
7 Replies

6. Shell Programming and Scripting

Common records after matching on different columns

Hi, I have the following files. cat 1.txt cat 2.txt output.txt The logic is as follows.... (10 Replies)
Discussion started by: jacobs.smith
10 Replies

7. Shell Programming and Scripting

Print columns matching to specific values

Hello Friends, I have a CDR file and i need to print out 2 columns with their field position which matches to some constant values, a part of input file CZ=1|CZA=1|DIAL=415483420001|EE=13|ESF=1|ET=|FF=0|9|MNC=99|MNP=9041|MTC=0|NID=2|NOA=international|ON=1| OutPut ... (3 Replies)
Discussion started by: EAGL€
3 Replies

8. Shell Programming and Scripting

Can't figure out how to find specific characters in specific columns

I am trying to find a specific set of characters in a long file. I only want to find the characters in column 265 for 4 bytes. Is there a search for that? I tried cut but couldn't get it to work. Ex. I want to find '9999' in column 265 for 4 bytes. If it is in there, I want it to print... (12 Replies)
Discussion started by: Drenhead
12 Replies

9. UNIX for Dummies Questions & Answers

Printing lines with specific strings at specific columns

Hi I have a file which is tab-delimited. Now, I'd like to print the lines which have "chr6" string in both first and second columns. Could anybody help? (3 Replies)
Discussion started by: a_bahreini
3 Replies

10. Shell Programming and Scripting

Matching first 2 columns..

Hello All, I want to make a file which will have primarily lines of file2 but when first 2 fields matches with the file1 it should have those lines of file1.. example is as below.. file1 a;b;1 c;d f;e t;r;5 file2 b;g a;b c;d v;b f;e t;r (2 Replies)
Discussion started by: ailnilanjan
2 Replies
File::Copy(3pm) 					 Perl Programmers Reference Guide					   File::Copy(3pm)

NAME
File::Copy - Copy files or filehandles SYNOPSIS
use File::Copy; copy("file1","file2"); copy("Copy.pm",*STDOUT);' move("/dev1/fileA","/dev2/fileB"); use POSIX; use File::Copy cp; $n = FileHandle->new("/a/file","r"); cp($n,"x");' DESCRIPTION
The File::Copy module provides two basic functions, "copy" and "move", which are useful for getting the contents of a file from one place to another. o The "copy" function takes two parameters: a file to copy from and a file to copy to. Either argument may be a string, a FileHandle ref- erence or a FileHandle glob. Obviously, if the first argument is a filehandle of some sort, it will be read from, and if it is a file name it will be opened for reading. Likewise, the second argument will be written to (and created if need be). Trying to copy a file on top of itself is a fatal error. Note that passing in files as handles instead of names may lead to loss of information on some operating systems; it is recommended that you use file names whenever possible. Files are opened in binary mode where applicable. To get a consistent behaviour when copy- ing from a filehandle to a file, use "binmode" on the filehandle. An optional third parameter can be used to specify the buffer size used for copying. This is the number of bytes from the first file, that wil be held in memory at any given time, before being written to the second file. The default buffer size depends upon the file, but will generally be the whole file (up to 2Mb), or 1k for filehandles that do not reference files (eg. sockets). You may use the syntax "use File::Copy "cp"" to get at the "cp" alias for this function. The syntax is exactly the same. o The "move" function also takes two parameters: the current name and the intended name of the file to be moved. If the destination already exists and is a directory, and the source is not a directory, then the source file will be renamed into the directory specified by the destination. If possible, move() will simply rename the file. Otherwise, it copies the file to the new location and deletes the original. If an error occurs during this copy-and-delete process, you may be left with a (possibly partial) copy of the file under the destination name. You may use the "mv" alias for this function in the same way that you may use the "cp" alias for "copy". File::Copy also provides the "syscopy" routine, which copies the file specified in the first parameter to the file specified in the second parameter, preserving OS-specific attributes and file structure. For Unix systems, this is equivalent to the simple "copy" routine, which doesn't preserve OS-specific attributes. For VMS systems, this calls the "rmscopy" routine (see below). For OS/2 systems, this calls the "syscopy" XSUB directly. For Win32 systems, this calls "Win32::CopyFile". On Mac OS (Classic), "syscopy" calls "Mac::MoreFiles::FSpFileCopy", if available. Special behaviour if "syscopy" is defined (OS/2, VMS and Win32) If both arguments to "copy" are not file handles, then "copy" will perform a "system copy" of the input file to a new output file, in order to preserve file attributes, indexed file structure, etc. The buffer size parameter is ignored. If either argument to "copy" is a handle to an opened file, then data is copied using Perl operators, and no effort is made to preserve file attributes or record structure. The system copy routine may also be called directly under VMS and OS/2 as "File::Copy::syscopy" (or under VMS as "File::Copy::rmscopy", which is the routine that does the actual work for syscopy). rmscopy($from,$to[,$date_flag]) The first and second arguments may be strings, typeglobs, typeglob references, or objects inheriting from IO::Handle; they are used in all cases to obtain the filespec of the input and output files, respectively. The name and type of the input file are used as defaults for the output file, if necessary. A new version of the output file is always created, which inherits the structure and RMS attributes of the input file, except for owner and protections (and possibly timestamps; see below). All data from the input file is copied to the output file; if either of the first two parameters to "rmscopy" is a file handle, its position is unchanged. (Note that this means a file handle pointing to the output file will be associated with an old version of that file after "rmscopy" returns, not the newly created version.) The third parameter is an integer flag, which tells "rmscopy" how to handle timestamps. If it is < 0, none of the input file's time- stamps are propagated to the output file. If it is > 0, then it is interpreted as a bitmask: if bit 0 (the LSB) is set, then time- stamps other than the revision date are propagated; if bit 1 is set, the revision date is propagated. If the third parameter to "rmscopy" is 0, then it behaves much like the DCL COPY command: if the name or type of the output file was explicitly specified, then no timestamps are propagated, but if they were taken implicitly from the input filespec, then all timestamps other than the revision date are propagated. If this parameter is not supplied, it defaults to 0. Like "copy", "rmscopy" returns 1 on success. If an error occurs, it sets $!, deletes the output file, and returns 0. RETURN
All functions return 1 on success, 0 on failure. $! will be set if an error was encountered. NOTES
o On Mac OS (Classic), the path separator is ':', not '/', and the current directory is denoted as ':', not '.'. You should be careful about specifying relative pathnames. While a full path always begins with a volume name, a relative pathname should always begin with a ':'. If specifying a volume name only, a trailing ':' is required. E.g. copy("file1", "tmp"); # creates the file 'tmp' in the current directory copy("file1", ":tmp:"); # creates :tmp:file1 copy("file1", ":tmp"); # same as above copy("file1", "tmp"); # same as above, if 'tmp' is a directory (but don't do # that, since it may cause confusion, see example #1) copy("file1", "tmp:file1"); # error, since 'tmp:' is not a volume copy("file1", ":tmp:file1"); # ok, partial path copy("file1", "DataHD:"); # creates DataHD:file1 move("MacintoshHD:fileA", "DataHD:fileB"); # moves (don't copies) files from one # volume to another AUTHOR
File::Copy was written by Aaron Sherman <ajs@ajs.com> in 1995, and updated by Charles Bailey <bailey@newman.upenn.edu> in 1996. perl v5.8.0 2002-06-01 File::Copy(3pm)
All times are GMT -4. The time now is 03:25 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy