Replace from taking from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace from taking from file
# 1  
Old 05-14-2014
Replace from taking from file

Hi,

I have one file. S_CHAR.cfg. Content given below.

Code:
\" abcd1234
\| abcd1235
\$ abcd1236

So it has two record per line delimited by space.

No I am looking for some help on how to replace this special charaters ( mentioned in record 1 of each line) with the values mentioned in S_CHAR.cfg file. ( 2nd record).

So for example if I have one file TEST.out with the content like

Code:
Great \"India\"
Great \"World\"
I am loving every \$ I earn.

After replacing it will look like
Code:
Great abcd1234Indiaabcd1234
Great abcd1234Worldabcd1234
I am loving every abcd1236 I earn.

Thanks in advance.

Last edited by Anupam_Halder; 05-14-2014 at 03:58 AM..
# 2  
Old 05-14-2014
Replace them where? In the same file? What is your expected output?
# 3  
Old 05-14-2014
If you want to replace the entire 1st field with 2nd,
Code:
awk '$1 = $2' file

# 4  
Old 05-14-2014
Sorry for incomplete requirement. Please check now. I have updated my post.
# 5  
Old 05-14-2014
Code:
awk 'NR == FNR{a[$1] = $2; next}
  {for(x in a)
    {gsub(x, a[x])};
    print}' S_CHAR.cfg TEST.out

These 2 Users Gave Thanks to SriniShoo For This Post:
# 6  
Old 05-14-2014
Hi ,
Will it be performance wise good, if I take one loop and replace 1st records with 2nd globally using SED?

---------- Post updated at 12:55 PM ---------- Previous update was at 12:49 PM ----------

Quote:
Originally Posted by SriniShoo
Code:
awk 'NR == FNR{a[$1] = $2; next}
  {for(x in a)
    {gsub(x, a[x])};
    print}' S_CHAR.cfg TEST.out

One problem with this. It's keeping the "\" in final output.

Output I got:
Code:
Great \a1b45cd56India\a1b45cd56
Great \a1b45cd56World\a1b45cd56
I am loving every \a1b45cd57 I earn.

Also it's replacing normal quotes(") and dollar($).

Last edited by Anupam_Halder; 05-14-2014 at 04:44 AM..
# 7  
Old 05-14-2014
Code:
awk 'NR == FNR{a[$1] = $2; next}
  {for(x in a)
    {gsub(x, a[x])}; gsub(/\\/, X);
    print}' S_CHAR.cfg TEST.out

These 2 Users Gave Thanks to SriniShoo For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search pattern in a file taking input from another file

Hi, Below is my requirement File1: svasjsdhvassdvasdhhgvasddhvasdhasdjhvasdjsahvasdjvdasjdvvsadjhv vdjvsdjasvdasdjbasdjbasdjhasbdasjhdbjheasbdasjdsajhbjasbjasbhddjb svfsdhgvfdshgvfsdhfvsdadhfvsajhvasjdhvsajhdvsadjvhasjhdvjhsadjahs File2: sdh hgv I need a command such that... (8 Replies)
Discussion started by: imrandec85
8 Replies

2. UNIX for Dummies Questions & Answers

Taking word count from file and printing in file

hi, i am having a file which contains the below content, i need to take the word count of if and print the file name also inputfile.txt file_name1.txt,type_name1.txt file_name2.txt,type_name2.txt i would need the word count of the files like this if file_name*.txt then wc -l... (10 Replies)
Discussion started by: rohit_shinez
10 Replies

3. Shell Programming and Scripting

Taking a specific value from a log file

Dear community, I've a file contaning some logs like: 185413.854: 185456.748: 185457.631: 185467.213: 185468.913: 185472.378: 185479.944: 185482.828: 185486.855: 185490.946: 185497.580: 185501.771: 185501.787: 185511.343: 185513.458: 3101902K(4089472K), 0.0117240 secs]... (6 Replies)
Discussion started by: Lord Spectre
6 Replies

4. Shell Programming and Scripting

Installing a bin file by taking input from a properties file

I need to install a bin file in UNIX which requires user interaction for giving some information like user id , path, sid etc. All these information is stored in a properties file in the same location. So if i give ./file.bin -f propfile.properties will it install the file taking all the... (1 Reply)
Discussion started by: coolmohere
1 Replies

5. Shell Programming and Scripting

How to ftp multiple files by taking the file name from a input file.

Hi, I'm working on a script which has to copy multiple files from one server to another server. The list of files that are to be copied is present in a file say input.txt. vi input.txt abc.c welcome.c new.c welcome1.c for ftp'ing a single file say 'new.c' the following code... (2 Replies)
Discussion started by: i.srini89
2 Replies

6. Shell Programming and Scripting

script for taking the stats from a file

i have file which contains data like this every day.i need to pull up a report for counting the 203's in that file for each subscriber id.there are around 200 subscriber id's. all ths Y's which i have written in the script are the subscriber id's.could some one give me an idea as to how do it in... (22 Replies)
Discussion started by: archana234
22 Replies

7. Shell Programming and Scripting

Taking a particular attribute's value in a file...

HI, Iam new to scripting..I need some guidance for invoking the following logic.. My input file contains Event Time = Thu, Nov 6, 2008 08:41:56 AM Probable Cause = UnspecifiedReason Specific Problems = BTS down due to Tx ... (16 Replies)
Discussion started by: Llb
16 Replies

8. Shell Programming and Scripting

taking a part from file name

hi i have a file with name "CT_Thread_Trace.384.20080728-121217.log" i need 384 , can any one help thanks Satya (5 Replies)
Discussion started by: Satyak
5 Replies

9. UNIX for Dummies Questions & Answers

Please help me in taking the values from a file

i have a script, whcih takes values from a file and assign it to one variable. this text value contains name of few object. value contains line by lie. i.e. one after another. when i am assaign the value of this text file to a variable, it accept the value as one liner instead of one by one.... (3 Replies)
Discussion started by: jin
3 Replies

10. UNIX for Dummies Questions & Answers

taking snapshot of a file

Hi I would like to take snapshots or look at a file every few secs that its not being edited or not being made any changes. After a minute, if there were no changes, then I would like to call my application job. Looking for command that does this. No success in googling it. the test... (1 Reply)
Discussion started by: bryan
1 Replies
Login or Register to Ask a Question