Sponsored Content
Top Forums Shell Programming and Scripting replace blank field in file 2 with content of file 1 Post 302553427 by durden_tyler on Wednesday 7th of September 2011 01:42:00 PM
Old 09-07-2011
Code:
$
$ cat file1
1234~abc~b~c~d~e~f~g~h~09/10/09
5678~def~b~c~d~e~f~g~h~12/06/10
8910~hij~b~c~d~e~f~g~h~03/28/13
1023~klm~b~c~d~e~f~g~h~07/29/11
$
$ cat file2
1234~ ~b~c~d~e~f~g~h~09/10/09
1234~ ~b~c~d~e~f~g~h~11/12/10
1234~ ~b~c~d~e~f~g~h~12/06/10
5678~ ~b~c~d~e~f~g~h~01/11/11
5678~ ~b~c~d~e~f~g~h~03/28/13
8910~ ~b~c~d~e~f~g~h~07/29/11
1023~ ~b~c~d~e~f~g~h~10/22/09
1023~ ~b~c~d~e~f~g~h~04/22/14
$
$ awk -F[~] 'BEGIN {OFS="~"} NR == FNR {x[$1] = $2} NR != FNR {$2=x[$1]; print}' file1 file2
1234~abc~b~c~d~e~f~g~h~09/10/09
1234~abc~b~c~d~e~f~g~h~11/12/10
1234~abc~b~c~d~e~f~g~h~12/06/10
5678~def~b~c~d~e~f~g~h~01/11/11
5678~def~b~c~d~e~f~g~h~03/28/13
8910~hij~b~c~d~e~f~g~h~07/29/11
1023~klm~b~c~d~e~f~g~h~10/22/09
1023~klm~b~c~d~e~f~g~h~04/22/14
$
$
$
$ perl -F[~] -lane '$ARGV eq "file1" ? $x{$F[0]}=$F[1] : print join "~",($F[0],$x{$F[0]},@F[2..$#F])' file1 file2
1234~abc~b~c~d~e~f~g~h~09/10/09
1234~abc~b~c~d~e~f~g~h~11/12/10
1234~abc~b~c~d~e~f~g~h~12/06/10
5678~def~b~c~d~e~f~g~h~01/11/11
5678~def~b~c~d~e~f~g~h~03/28/13
8910~hij~b~c~d~e~f~g~h~07/29/11
1023~klm~b~c~d~e~f~g~h~10/22/09
1023~klm~b~c~d~e~f~g~h~04/22/14
$
$

tyler_durden

Last edited by durden_tyler; 09-07-2011 at 02:52 PM..
This User Gave Thanks to durden_tyler For This Post:
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to replace a character with blank in a file

hi, I have a doubt in replacing characters with blank. My requirement is that, i have one file and looks like below 4:ALTER SYSTEM DISCONNECT SESSION '193,191' IMMEDIATE; 6:ALTER SYSTEM DISCONNECT SESSION '205,7274' IMMEDIATE; 5:ALTER SYSTEM DISCONNECT SESSION '206,34158' IMMEDIATE;... (4 Replies)
Discussion started by: sridhusha
4 Replies

2. Shell Programming and Scripting

Find and replace blank in the last field

Hi all, I have a huge file and I need to get ride of the fields 6-11 and replace the blanks in field 5 with a missing value(99999). 159,93848,5354,343,67898,45,677,5443,434,5545,45 677,45545,3522,244, 554,54344,3342,456, 344,43443,2344,444,23477... (12 Replies)
Discussion started by: GoldenFire
12 Replies

3. Shell Programming and Scripting

Text file to CSV with field data separated by blank lines

Hello, I have some data in a text file where fields are separated by blank lines. There are only 6 fields however some fields have several lines of data as I will explain. Also data in a particular field is not consistently the same size but does end on a blank line. The first field start with... (6 Replies)
Discussion started by: vestport
6 Replies

4. Shell Programming and Scripting

Adding an additional blank field to a file

Hi, I have the following file, I'd like to add an additional blank field to this file This is a tab delimited file, I have tried the same thing on excel, but looking for a unix solution. Here is my input: Country Postal Admin4 StreetBaseName StreetType HUN 2243 Kóka Dózsa György ... (3 Replies)
Discussion started by: ramky79
3 Replies

5. Shell Programming and Scripting

Sed: replace content from file with the content from file

Hi, I am having trouble while using 'sed' with reading files. Please help. I have 3 files. File A, file B and file C. I want to find content of file B in file A and replace it by content in file C. Thanks a lot!! Here is a sample of my question. e.g. (file A: a.txt; file B: b.txt; file... (3 Replies)
Discussion started by: dirkaulo
3 Replies

6. Shell Programming and Scripting

Find a blank field and replace values to NA

Hi All, i have a file like col1 col2 col3 13 24 NA 12 13 14 11 12 13 14 22 NA 18 26 NA in this file if i found "NA" other values in the line are also replace by NA Could you help me! (7 Replies)
Discussion started by: Shenbaga.d
7 Replies

7. Shell Programming and Scripting

Replace string in a file with some content indexed from another file.

We have two files file 1: (usually small, ~100 lines), each line contains a : separated index, value e.g 2: Apple 1: Banana 5: Pear 7: Orange File 2: (usually large, 10 million lines or more), each line contains a single string value. e.g xyz1 xyz2 xyz3 xyz4 xyz5 xyz6 xyz7 Now... (2 Replies)
Discussion started by: AlokKumbhare
2 Replies

8. Shell Programming and Scripting

How to replace blank tab with zero in a file?

hi, i need to replace a blank tab output in a file to zero. input file: 2015/08/04 00:00:00 171 730579 27088 <blank> 3823 30273 1621778 ... (6 Replies)
Discussion started by: amyt1234
6 Replies

9. UNIX for Dummies Questions & Answers

Conditionally replace field content

Hello I am trying to conditionally replace field content in $2 of input file if pattern is found in $4 of the same tab-separated input file. Currently, $2 is empty. I am trying (with no success): awk -F "\t" 'BEGIN {FS="\t"}{ if ($4=="NO") $2=$2"NO"; print $0}' in > out (2 Replies)
Discussion started by: dovah
2 Replies
comm(1) 							   User Commands							   comm(1)

NAME
comm - select or reject lines common to two files SYNOPSIS
comm [-123] file1 file2 DESCRIPTION
The comm utility reads file1 and file2, which must be ordered in the current collating sequence, and produces three text columns as output: lines only in file1; lines only in file2; and lines in both files. If the input files were ordered according to the collating sequence of the current locale, the lines written will be in the collating sequence of the original lines. If not, the results are unspecified. OPTIONS
The following options are supported: -1 Suppresses the output column of lines unique to file1. -2 Suppresses the output column of lines unique to file2. -3 Suppresses the output column of lines duplicated in file1 and file2. OPERANDS
The following operands are supported: file1 A path name of the first file to be compared. If file1 is -, the standard input is used. file2 A path name of the second file to be compared. If file2 is -, the standard input is used. USAGE
See largefile(5) for the description of the behavior of comm when encountering files greater than or equal to 2 Gbyte ( 2**31 bytes). EXAMPLES
Example 1: Printing a list of utilities specified by files If file1, file2, and file3 each contain a sorted list of utilities, the command example% comm -23 file1 file2 | comm -23 - file3 prints a list of utilities in file1 not specified by either of the other files. The entry: example% comm -12 file1 file2 | comm -12 - file3 prints a list of utilities specified by all three files. And the entry: example% comm -12 file2 file3 | comm -23 -file1 prints a list of utilities specified by both file2 and file3, but not specified in file1. ENVIRONMENT VARIABLES
See environ(5) for descriptions of the following environment variables that affect the execution of comm: LANG, LC_ALL, LC_COLLATE, LC_CTYPE, LC_MESSAGES, and NLSPATH. EXIT STATUS
The following exit values are returned: 0 All input files were successfully output as specified. >0 An error occurred. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWesu | +-----------------------------+-----------------------------+ |CSI |enabled | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ SEE ALSO
cmp(1), diff(1), sort(1), uniq(1), attributes(5), environ(5), largefile(5), standards(5) SunOS 5.10 3 Mar 2004 comm(1)
All times are GMT -4. The time now is 06:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy