How to manipulate string in line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to manipulate string in line?
# 8  
Old 10-02-2018
Hello Vgersh99,

Quote:
Originally Posted by vgersh99
Sure thing.
Here's my idea with awk:
1. read your ComparisonFile into an array indexed by $0 (hint: FNR==NR)
2. for each odd line in MainFile, substituting first , by itself appended with the last field on a line followed by : . Set a flag
3. If flag is set and you're on an even line, output the line and reset the flag.
Regarding your remark 1, I did not understand what happens when we read the ComparisonFile into array but here you are:
Code:
awk 'NR==FNR{a[$0];}{if($0 in a)print $0}' ComparisonFile > ReadArray

Do not understand how to use ReadArray file in this case.

Now, read the last column in Mainfile and if it's the same with $1 (line in ComparisonFile), replace , by ,$1::
Code:
While read line_ComparisonFile && read -r line_MainFile <&3; do
L=$(awk '{print $NF}' $line_MainFile)
awk '$line_ReadArray==$L '{gsub(/,/,"$line_ReadArray:")}' {f=1} f'
done < ComparisonFile 3<MainFile

This is what I learnt so far.

PS: As I could not append even/oddfunction into it, I do not know how flag will understand when it will be turned on/off.
Code:
awk 'NR%2==0' MainFile > even
awk 'NR%2==1' MainFile > odd


Many thanks
Boris

Last edited by baris35; 10-02-2018 at 05:55 PM.. Reason: even/odd missed
This User Gave Thanks to baris35 For This Post:
# 9  
Old 10-02-2018
here's the code demonstrating the algorithm described in my previous comment - line by line:
Code:
awk '
   FNR==NR{f2[$1];next} 
   FNR%2 { if ($NF in f2) {sub(",","&" $NF ": ",$1);$NF=""; f=1; print;next} }
   f {print $0; f--}' ComparisonFile MainFile

this can be further "simplified":
Code:
awk '
  FNR==NR{ f2[$1];next }
  FNR%2 { if ($NF in f2) {sub(",","&" $NF ": ",$1);$NF=""; f=1; print;next} } 
  f&&f--' ComparisonFile MainFile


Last edited by vgersh99; 10-02-2018 at 08:55 PM..
This User Gave Thanks to vgersh99 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Red Hat

How to add a new string at the end of line by searching a string on the same line?

Hi, I have a file which is an extract of jil codes of all autosys jobs in our server. Sample jil code: ************************** permission:gx,wx date_conditions:yes days_of_week:all start_times:"05:00" condition: notrunning(appDev#box#ProductLoad)... (1 Reply)
Discussion started by: raghavendra
1 Replies

2. Shell Programming and Scripting

Manipulate XML File Continous STRING by each Order Line using SHELL

heres sample File: <?xml version="1.0"?> <!DOCTYPE cXML SYSTEM "www"><cXML.............................................. <OrderRequest>USE UNIX.com</Extrinsic><Extrinsic name="UniqueName">Peter@UNIX.com</Extrinsic><Extrinsic name="ContractingEntity">UNIX... (3 Replies)
Discussion started by: Pete.kriya
3 Replies

3. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

4. Shell Programming and Scripting

Find string in one file and manipulate other

hi, I have 2 files delimited by "|" File 1: 1|28|ABC|11|9620034||XXX555| 29|22|ABC|11|9620258||XXX555| 51|26|ABC|11|9620314||XXX555| 77|20|ABC|11|9630506||XXX555| 97|36|ABC|11|9630562||XXX555| File 2: 9620028|I 9620034|I 9620314|S 9620332|I 9620258|I 9630506|S 9630562|S (3 Replies)
Discussion started by: pparthiv
3 Replies

5. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

6. Shell Programming and Scripting

Manipulate string in shell script

I am writing a shell script for some purpose. I have a variable of the form -- var1 = "policy=set policy" Now I need to manipulate the variable var to get the string after index =. that is i should have "set polcy". Also I need to to this for many other variables where the value of "=" is not... (3 Replies)
Discussion started by: Dev_Sharma987
3 Replies

7. Shell Programming and Scripting

Extract File line and manipulate

How can I print a section of each line in a text file. Eg CODE1 XYR Test2 10319389 CODE2 XYR Test2 10319389 CODE3 XYR Test2 10319389 CODE4 XYR Test2 10319389 CODE5 XYR Test2 10319389 First thing that would be nice would a new file like, awk sed and substring may help but can't figure it... (6 Replies)
Discussion started by: kelseyh
6 Replies

8. Shell Programming and Scripting

How to manipulate first column and reverse the line order in third and fourth column?

How to manipulate first column and reverse the line order in third and fourth column as follws? For example i have a original file like this: file1 0.00000000E+000 -1.17555359E-001 0.00000000E+000 2.00000000E-002 -1.17555359E-001 0.00000000E+000 ... (1 Reply)
Discussion started by: Max Well
1 Replies

9. UNIX for Dummies Questions & Answers

To manipulate a specific line

Hi, I would like to cut a specific line from a text file and then manipulate the text in that line. For eg. below is "tmp" file. ----------------- Tue 07/05/05 00:27:34.333 Tue 07/05/05 00:27:34.333 4 events were processed for customer 315 and will be correctly resolved when 315MERGE is run:... (2 Replies)
Discussion started by: dhiman.sarkar
2 Replies
Login or Register to Ask a Question