replace the contents of first column of file "X" with second Column of file "X" in file "Y"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace the contents of first column of file "X" with second Column of file "X" in file "Y"
# 8  
Old 11-30-2015
tried for perl script also but of no use.

If any one can help me with changing the UIDs only without changing the format of the file.

---------- Post updated at 01:27 AM ---------- Previous update was at 01:03 AM ----------

why can't we search a string in a file and substitute with a string from another file.
Why is it we have to make a regular expression containing Name / Uid.
why can't a string be searched line by line in a file without any dependency.

Last edited by shahid1632; 11-30-2015 at 03:40 PM.. Reason: not working
# 9  
Old 11-30-2015
Quote:
Originally Posted by shahid1632
tried for perl script also but of no use.

If any one can help me with changing the UIDs only without changing the format of the file.

---------- Post updated at 01:27 AM ---------- Previous update was at 01:03 AM ----------

why can't we search a string in a file and substitute with a string from another file.
Why is it we have to make a regular expression containing Name / Uid.
why can't a string be searched line by line in a file without any dependency.
I am sorry the Perl script is of no use to you. If you post in which way it failed I might be able to know why.

The answer to all your "why(s)" is this. I do not know how your real data looks like, I can only go by what you have posted, and as I have shown you in post #7,
according to the information you have given, the result is as requested. You do not want to allow the substitution to occur in lines that it is not intended for, inadvertently.
If you think that's the problem, please, just remove the extra condition:

Change if($blocks[1] and $blocks[0]=~/^Name|^Uid/){ to if($blocks[1]){

Quote:
If any one can help me with changing the UIDs only without changing the format of the file.
Please, show us how the format is changed. My script does not change any format. It leaves spaces and symbols and newlines as found.
# 10  
Old 12-01-2015
To reply to your three "why"s:
1) !t can be and has been done; any of the solutions provided does exactly this.
2) For safety reasons. If you're absolutely sure there won't be any false positives, the name/uid check can be dropped.
3) see above.

As the solutions provided DO work when tested locally, I suspect your file has unusual structures, e.g. hidden characters like (but not limited to) the DOS <CR> (0x0D, \r, ^M) line terminators.
# 11  
Old 12-01-2015
Thanks all of you, but i found a simple solution to my problem :

Firstly I added
Code:
 sed -i -e 's/ column 1 of file 'X' / column 2 of file 'X'/g' file name Y

to all the rows in the excel file X. Then made a text file X.
Then made a script myscript and executed ./myscript

Last edited by Don Cragun; 12-01-2015 at 07:40 AM.. Reason: Fix CODE tags.
# 12  
Old 12-01-2015
Quote:
Originally Posted by shahid1632
Thanks all of you, but i found a simple solution to my problem :

Firstly I added
Code:
 sed -i -e 's/ column 1 of file 'X' / column 2 of file 'X'/g' file name Y

to all the rows in the excel file X. Then made a text file X.
Then made a script myscript and executed ./myscript
Hi shahid1632

You never mentioned before that file X was an excel file. If this means that it came from a msdos world, your file X does need to have the hidden \r removed. Before using it, it should be ran through something like:

Code:
tr -d '\r' < fileX > patternsX

Now you can use file patternsX with any of the scripts previously given to you.

Concerning your "found" solution:
Quote:
Code:
 sed -i -e 's/ column 1 of file 'X' / column 2 of file 'X'/g' file name Y

You do not need the g at the end of the sed expression, unless you expect column 1 of file 'X' to appear several times in the same line in file name Y

Also, the only way that it might have worked is if you manually created an entry for each of the 1000 patterns in file X, which begs the question why not to manually do the replacement in file Y. It does not make any sense to fabricate 1000 shovels to dig a 1000 holes.
# 13  
Old 12-01-2015
Sorry, Its my fault. I didn't told you that

the file X, contains many occurrences of column 1 value.

Second, The file X was a text file which was manually created by copying the contents from excel file.
Third, as you must be aware that in excel there is an option of dragging the contents to the last row so there was negligible effort involved in making the mysript from already file X which was in excel.
Thanks to all of you once again.
For the sake of other readers you can wrap up by posting the perl script once again.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Replacing "." with "GG" in a certain column of a file that has heading

Hi, all, I have a file that looks like: ## XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ## YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY #AA AB AC AD AE AF AG AH AI AJ AK AL 20 60039 60039 ... (5 Replies)
Discussion started by: kush
5 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

finding the strings beween 2 characters "/" & "/" in .txt file

Hi all. I have a .txt file that I need to sort it My file is like: 1- 88 chain0 MASTER (FF-TE) FFFF 1962510 /TCK T FD2TQHVTT1 /jtagc/jtag_instreg/updateinstr_reg_1 dff1 (TI,SO) 2- ... (10 Replies)
Discussion started by: Behrouzx77
10 Replies

5. Shell Programming and Scripting

Substituting comma "," for dot "." in a specific column when comma"," is a delimiter

Hi, I'm dealing with an issue and losing a lot of hours figuring out how i would solve this. I have an input file which looks like this: ('BLABLA +200-GRS','Serviço ','TarifaçãoServiço','wap.bla.us.0000000121',2985,0,55,' de conversão em escada','Dia','Domingos') ('BLABLA +200-GRR','Serviço... (6 Replies)
Discussion started by: poliver
6 Replies

6. UNIX for Dummies Questions & Answers

replace "," with "." only in specific columns of a file?

Hi all, I have this text file containing 9 columns separated by space. The 8th columns contains the numbers. C1 C2 C3 C4 C5 C6 C7 C8 C9 er rt yt gh iu nk il 0.07 xs yt lr ty bg iu zk nh 0,0005 lt ...etc. I want to replace the comma with full stop only in 8th coloumn. the output... (8 Replies)
Discussion started by: Unilearn
8 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question