How to manipulate string in line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to manipulate string in line?
# 1  
Old 10-01-2018
How to manipulate string in line?

Hello,
I looked up on google but do not know from which point to start...
I am under ubuntu 18 bionic and Mainfile consists of 25K lines

MainFile:
Code:
Test,AAEE9FED3, GGBBDD DD AA X2d Moscow
112233445566aaBBccPPdddEE
Test,AAEE9FED3, GG33DD s00022 Leningrad
11298932566aaBBccPPdddEE
Test,AAEE9FED3, 33VVDD sdsds333 Belgorod
11090aBBccPPdSDSDEw00
Test,AAEE9FED3, QQTT11 00DD2 Astrakhan
112233445566aaBBccPPdddEE
Test,SDFEE3D3, SDPL31 00DD2 Buryatiya
112233445566aaBBccPPdddEE
..
..
..

ComparisonFile:
Code:
Moscow
Leningrad
Astrakhan

I wish to convert the MainFile into below format:
Code:
Test,Moscow: AAEE9FED3, GGBBDD DD AA X2d
112233445566aaBBccPPdddEE
Test,Leningrad: AAEE9FED3, GG33DD s00022
11298932566aaBBccPPdddEE
Test,Astrakhan: AAEE9FED3, QQTT11 00DD2 
112233445566aaBBccPPdddEE

I thought that the algorithm should be like this:

1) read the line in the comparison file,
2) search space_$line in MainFile
3) if it returns, cut space_$line in MainFile ...just in case "$line" is coming after the last space in MainFile
4) put $line right after Test, phrase

A bit complicated.

I'd appreciate your help

Many thanks
Boris
# 2  
Old 10-01-2018
any idea how to do it purely in awk?
# 3  
Old 10-01-2018
Dear Vgersh99,
My idea:
while read comparison file, grep each matching line > create a new file but then when I am gonna paste two files, it will fail I think. I do not like awk as I do not understand and unable to edit in my future needs. I suppose I need to learn how stuff works with awk command.

Please do not reply promptly. I am trying to learn awk command. Just let the baby crowl on the ground for 24h. Many thanks

Kind regards
Boris

Last edited by baris35; 10-01-2018 at 06:25 PM..
# 4  
Old 10-01-2018
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.
# 5  
Old 10-01-2018
start with:
Code:
while read line
do
   last_word=${line##* }
   word=0
   grep -q -i "^$last_word$" ComparisonFile && word=1
   if [ $word = 1 ]
   then
      line=${line/Test,/Test,$last_word: }
      echo "${line% *}"
   else
      echo "${line}"
   fi
done < MainFile


Last edited by rdrtx1; 10-01-2018 at 08:25 PM.. Reason: updated echo.
# 6  
Old 10-01-2018
Quote:
Originally Posted by rdrtx1
Code:
while read line
do
   last_word=${line##* }
   grep -q -i "$last_word" ComparisonFile && {
      line="${line/Test,/Test,$last_word: }"
   }
   echo "$line"
done < MainFile

I get this - not exactly what the OP was after....
Code:
Test,Moscow: AAEE9FED3, GGBBDD DD AA X2d Moscow
112233445566aaBBccPPdddEE1
Test,Leningrad: AAEE9FED3, GG33DD s00022 Leningrad
11298932566aaBBccPPdddEE2
Test,AAEE9FED3, 33VVDD sdsds333 Belgorod
11090aBBccPPdSDSDEw00
Test,Astrakhan: AAEE9FED3, QQTT11 00DD2 Astrakhan
112233445566aaBBccPPdddEE3
Test,SDFEE3D3, SDPL31 00DD2 Buryatiya
112233445566aaBBccPPdddEE

# 7  
Old 10-01-2018
@rdrtx1: still doesn't seem to suppress the records NOT in ComparisonFile, after the revision. And, it greps the ComparisonFIle 25k times ... might become lengthy.
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