Sponsored Content
Top Forums Shell Programming and Scripting Replace Contents from One file into another Post 302824851 by DGPickett on Friday 21st of June 2013 05:19:45 PM
Old 06-21-2013
Well, 'paste' is OK for identically keyed and sorted files, but 'join' is a more general solution (with sort to ensure it is key sorted, sometimes needing LC_ALL=C to make sort binary). Or awk could store one file in an associative vector and then look the right lines up to match with the lines of the second file, sort order not an issue but you need unique keys in file 1, as it a only a one to many join.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace contents of a file

Hi, I want to replace the contents of a file.I tried using : sed 's/01514581/01514582/' $p where 01514581 is the original value 01514582 is the replaced value $p is the file name (captured in a variable).. The output does not recognise $p If you give : sed... (2 Replies)
Discussion started by: shiroh_1982
2 Replies

2. Shell Programming and Scripting

Compare & replace contents within a file

I have 2 files file1 1 TMQUEUE QUE1 STMW633A 100 DMADM DOMGRPSTMW633A STMW633A 100 GWADM GWTGRPSTMW633A STMW633A 100 GWADM GWTGRPSTMW633AA STMW633A 100 GWADM GWTGRPSTMW638A STMW638A 100 TMSYSEVT EVTGRPSTMW633A STMW633A 100 TMSYSEVT ... (2 Replies)
Discussion started by: kaustubh137
2 Replies

3. Shell Programming and Scripting

Script to check a file and replace some of the contents

Hi I have a file that looks like this: Line 0 animal elephant Line 1 animal elephant Line 2 animal elephant Line 3 animal elephant What i am aiming to do is with a script and an input value of... (6 Replies)
Discussion started by: tara
6 Replies

4. Shell Programming and Scripting

How to replace specific contents in a file?

From the existing file, I need to replace specific contents possibly with var every time when the user changes the var. e.g the contents in the file file.txt is 'My name is $n and I am $y years old' and every time user changed the var outside the file, the contents of the file should be created... (4 Replies)
Discussion started by: Emilywu
4 Replies

5. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

6. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

7. Shell Programming and Scripting

Replace file contents from another

Hi Friends, I have a file1 with 5 columns a b c d e f g h i j I have file2 with 3 columns 1 2 3 4 5 6 I want to replace 3rd 4th and 5th columns in file1 with file2 contents, so the output would be a b 1 2 3 f g 4 5 6 Thanks (6 Replies)
Discussion started by: jacobs.smith
6 Replies

8. UNIX for Dummies Questions & Answers

How To Replace Contents in a File?

How can i replace the contents in a particular line of a file. <FOLDERMAP SOURCEFOLDERNAME="FFCB-2012" SOURCEREPOSITORYNAME="Repo_DEV" TARGETFOLDERNAME="TEST" TARGETREPOSITORYNAME="Dev_Repo"/> For Example I want to replace the SOURCEREPOSITORYNAME="Repo_DEV" to... (3 Replies)
Discussion started by: Ariean
3 Replies

9. Shell Programming and Scripting

Replace contents of a file

Hello, I need help to replace a value by a new one. I've got a script, that will get directory size in ko, then write the size value return in a log file : The patch is given in crontab, and use with $1 in the script. I am looking for help to replace, after: the old value without to... (6 Replies)
Discussion started by: Aswex
6 Replies

10. Shell Programming and Scripting

sed - Replace string with file contents

Hello, I have two files: file1 and file2 file1 has the following info: --- host: "localhost" port: 3000 reporter_type: "zookeeper" zk_hosts: - "localhost:2181" file2 contains an IP address (1.1.1.1) What I want to do is replace localhost with 1.1.1.1, so that the... (4 Replies)
Discussion started by: Jay Kah
4 Replies
sort(1) 						      General Commands Manual							   sort(1)

Name
       sort - sort file data

Syntax
       sort [options] [-k keydef] [+pos1[-pos2]] [file...]

Description
       The  command  sorts  lines  of  all the named files together and writes the result on the standard output.  The name `-' means the standard
       input.  If no input files are named, the standard input is sorted.

Options
       The default sort key is an entire line.	Default ordering is lexicographic by  bytes  in  machine  collating  sequence.	 The  ordering	is
       affected globally by the following options, one or more of which may appear.

       -b	   Ignores leading blanks (spaces and tabs) in field comparisons.

       -d	   Sorts data according to dictionary ordering:  letters, digits, and blanks only.

       -f	   Folds uppercase to lowercase while sorting.

       -i	   Ignore characters outside the ASCII range 040-0176 in nonnumeric comparisons.

       -k keydef   The	keydefargument	is  a key field definition. The format is field_start, [field_end] [type], where field_start and field_end
		   are the definition of the restricted search key, and type is a modifier from the option list [bdfinr]. These modifiers have the
		   functionality, for this key only, that their command line counter-parts have for the entire record.

       -n	   Sorts fields with numbers numerically.  An initial numeric string, consisting of optional blanks, optional minus sign, and zero
		   or more digits with optional decimal point, is sorted by arithmetic value.  (Note that -0 is taken to be equal to 0.)  Option n
		   implies option b.

       -r	   Reverses the sense of comparisons.

       -tx	   Uses specified character as field separator.

       The  notation  +pos1 -pos2 restricts a sort key to a field beginning at pos1 and ending just before pos2.  Pos1 and pos2 each have the form
       m.n, optionally followed by one or more of the options bdfinr, where m tells a number of fields to skip from the beginning of the line  and
       n tells a number of characters to skip further.	If any options are present they override all the global ordering options for this key.	If
       the b option is in effect n is counted from the first nonblank in the field; b is attached independently to pos2.  A missing .n means .0; a
       missing	-pos2  means the end of the line.  Under the -tx option, fields are strings separated by x; otherwise fields are nonempty nonblank
       strings separated by blanks.

       When there are multiple sort keys, later keys are compared only after all earlier keys compare equal.  Lines that otherwise  compare  equal
       are ordered with all bytes significant.

       These are additional options:

       -c	   Checks sorting order and displays output only if out of order.

       -m	   Merges previously sorted data.

       -o name	   Uses specified file as output file.	This file may be the same as one of the inputs.

       -T dir	   Uses specified directory to build temporary files.

       -u	   Suppresses all duplicate entries.  Ignored bytes and bytes outside keys do not participate in this comparison.

Examples
       Print in alphabetical order all the unique spellings in a list of words.  Capitalized words differ from uncapitalized.
	       sort -u +0f +0 list

       Print the password file, sorted by user id number (the 3rd colon-separated field).
	       sort -t: +2n /etc/passwd

       Print the first instance of each month in an already sorted file of (month day) entries.  The options -um with just one input file make the
       choice of a unique representative from a set of equal lines predictable.
	       sort -um +0 -1 dates

Restrictions
       Very long lines are silently truncated.

Diagnostics
       Comments and exits with nonzero status for various trouble conditions and for disorder discovered under option c.

Files
       /usr/tmp/stm*, /tmp/*	first and second tries for temporary files

See Also
       comm(1), join(1), rev(1), uniq(1)

																	   sort(1)
All times are GMT -4. The time now is 01:47 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy