Removing string from CSV file by provide removal string from other file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing string from CSV file by provide removal string from other file
# 1  
Old 08-25-2017
Linux Removing string from CSV file by provide removal string from other file

What I need is to remove the text from Location_file.txt from each line matching all entries from Remove_location.txt

Location_file.txt
Code:
FlowPrePaid, h3nmg1cm2,Jamaica_MTAImageFileFlowPrePaid,h0nmg1cm1, Flow_BeatTest,FlowRockTest
FlowNewTest,FlowNewTest,h0nmg1cm1
PartiallySubscribed, grndustyc42,h0nmg1cm1,PartialSub_Feb9
FlowBeatTest,Flow_BeatTest,h0nmg1cm1
FlowJazzTest,FlowJazzTest, h10nmg1cm1copy
NodeMonitor,Node_Monitor,h0nmg1cm1
h10nmg1cm1copy,FlowUltimateTest,h0nmg1cm1,UltimateTest
FlowRockTest,FlowRockTest,h0nmg1cm1
FlowRaveTest,FlowRaveTest,h0nmg1cm1
FlowIgnitionTest,FlowIgnitionTest,h0nmg1cm1
FlowJazz, h3nmg1cm2,h0nmg1cm1, Flow_BeatTest,FlowRockTest
FlowAcceleratorTest,FlowAcceleratorTest, h3nmg1cm2


Remove_location.txt
Code:
h0nmg1cm1
grndustyc42
h10nmg1cm1copy
h3nmg1cm2

The code I have tried in recessive for loop is not working properly as for each value in Remove_location.txt, first iteration removes only first entry’ h0nmg1cm1’ from file Location_file.txt but for next value ‘grndustyc42’ its considering the initial file without effect/removal of value from previous iterations. Thus output file will always have all previously cleared entries..!

So how I can push new edited file each time with removed entries from Remove_location.txt.

second part of script is Removing duplicate lines and changing , to | also making copy of all files from name of first filed of final.txt to other location.

Code:
#!/bin/bash
>location_removed_out.txt
while read line
do
                while read cmts
                do
                        CMTS_VAL=$(echo $line | awk '{gsub(/'$cmts,*'/,"")}1')
                done < Remove_location.txt
#       echo "line value is : $line"
#       echo "cmts_val is : $CMTS_VAL"
        echo $CMTS_VAL >> location_removed_out.txt
done < Location_file.txt

#Removing duplicate lnes and changing ,  to |
awk '!seen[$0]++' location_removed_out.txt | tr "," "|" > final.txt

#Searching for file name and making copy

cd /home/webapps/project1/folder1
for f in `less final.txt | awk -F| "{print $1}'`
do 
   		file=$(echo $f)
		if [ -f "$file" ]
		then
		echo "$file found."
		   cp -v "$f" /home/webapps/project1/"${f%.xml}"_$(date +%m%d%y).csv
		else
			echo "$file not found.moving to next file....!" >> file_copyLog.txt
		fi

done


So Out put file will be below file and copy of files from field1 of this file
final.txt:
Code:
FlowPrePaid|Jamaica_MTAImageFileFlowPrePaid|Flow_BeatTest|FlowRockTest
FlowNewTest|FlowNewTest
PartiallySubscribed |PartialSub_Feb9
FlowBeatTest|Flow_BeatTest
FlowJazzTest|FlowJazzTest
NodeMonitor|Node_Monitor
FlowUltimateTest|UltimateTest
FlowRockTest|FlowRockTest
FlowRaveTest|FlowRaveTest
FlowIgnitionTest|FlowIgnitionTest
FlowJazz|Flow_BeatTest|FlowRockTest
FlowAcceleratorTest|FlowAcceleratorTest



Moderator's Comments:
Mod Comment
Please wrap all code, files, input and output/errors in CODE tags

Last edited by rbatte1; 08-25-2017 at 11:55 AM.. Reason: Added CODE tags
# 2  
Old 08-25-2017
As you deploy awk anyhow several times in your script, a single pass awk script may come in handy? Your usage / processing of spaces within or at end-of line doesn't seem to be consistent, so some deviation from your desired output may have to be absolved:
Code:
awk -F, -vOFS="|" 'NR == FNR {T[$1]; next} {for (t in T) gsub (t ",*|, *$", _); $1=$1}1' file2 file1 
FlowPrePaid| Jamaica_MTAImageFileFlowPrePaid| Flow_BeatTest|FlowRockTest
FlowNewTest|FlowNewTest
PartiallySubscribed| PartialSub_Feb9
FlowBeatTest|Flow_BeatTest
FlowJazzTest|FlowJazzTest
NodeMonitor|Node_Monitor
FlowUltimateTest|UltimateTest
FlowRockTest|FlowRockTest
FlowRaveTest|FlowRaveTest
FlowIgnitionTest|FlowIgnitionTest
FlowJazz|  Flow_BeatTest|FlowRockTest
FlowAcceleratorTest|FlowAcceleratorTest

As for the second part of the script, I'm afraid I didn't fully understand what you're after...?


EDIT: As there are no duplicates in your sample, I had to create a few; for their removal make the script
Code:
awk -F, -vOFS="|" 'NR == FNR {T[$1]; next} {for (t in T) gsub (t ",*|, *$", _); $1=$1} !seen[$0]++' file2 file1


Last edited by RudiC; 08-25-2017 at 05:57 PM..
This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-28-2017
thanks, working perfect..!

can you give me bit explanation how it works..
Code:
awk -F, -vOFS="|" 'NR == FNR {T[$1]; next} {for (t in T) gsub (t ",*|, *$", _); $1=$1} !seen[$0]++' file2 file1


Last edited by RudiC; 08-28-2017 at 07:38 AM..
# 4  
Old 08-28-2017
Code:
awk -F,                                                 # set input  field separator to ","
 -vOFS="|"                                              # set output field separator to "|"
'NR == FNR                                              # if processing first file (file line No. == stream line No.)
                {T[$1]                                  # save "remove location" as index in an (empty) array
                 next                                   # stop processing script for this line; start over with next line
                }
                                                        # now in second file
                {for (t in T)                           # loop through T's indices (awk feature)
                                gsub (t ",*|, *$", _)   # replace index string plus evtl. comma, or trailing comma with
                                                        # empty string (unassigned variable "_")
                 $1 = $1                                # Replace all comma field separators with "|". man awk:  "Assignment
                                                        # to NF or to a field causes $0 to be reconstructed by concatenating
                                                        # the $i's  separated  by  OFS."
                }
!seen[$0]++                                             # print first occurrences of lines only (remove duplicates) 
' file2 file1

This User Gave Thanks to RudiC 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

[bash] - Replace blank and string in csv file

Hi all, i have a .csv file with only two columns, like: Login;Status Luca;S Marco; Stefano; Elettra;S Laura; ... I need to replace the blank space on Status column whit Enabled end, on the same column, S whit Disabled, like: Login;Status Luca;Disabled Marco;Enabled Stefano;Enabled... (10 Replies)
Discussion started by: kamose
10 Replies

2. Shell Programming and Scripting

Print particular string in a field of csv file - part 2

Hi, all I need your help and suggestions. I want to print particular strings in a field of a csv file and show them in terminal. Here is an example of the csv file. SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw... (7 Replies)
Discussion started by: refrain
7 Replies

3. Shell Programming and Scripting

Print particular string in a field of csv file

Hi, all I need your help and suggestions. I want to print particular strings in a field of a csv file and show them in terminal. Here is an example of the csv file. SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw... (9 Replies)
Discussion started by: refrain
9 Replies

4. UNIX for Dummies Questions & Answers

Handling Comma in string values in a CSV file

Hi have a comma separated file which has numeric and string columns. String columns are quoted and can have comma in between the quotes. How to identify the columns with FS ="," sample records"prabhat,kumar",19,2000,"bangalore,India" In awk it should be$1 = prabhat,kumar $2=19 $3=2000... (9 Replies)
Discussion started by: prabhat.diwaker
9 Replies

5. Windows & DOS: Issues & Discussions

Removing anything from text file except specific string

So, I have a text file that looks like this: 0,0: (168,168,176) #A8A8B0 srgb(168,168,176) 1,0: (168,168,176) #A8A8B0 srgb(168,168,176) 2,0: (166,166,174) #A6A6AE srgb(166,166,174) 3,0: (166,166,174) #A6A6AE srgb(166,166,174) 4,0: (168,168,176) #A8A8B0 srgb(168,168,176) 5,0:... (0 Replies)
Discussion started by: pasc
0 Replies

6. Shell Programming and Scripting

String removal from file

Dear all From below mention input file I needed op file as show below. I am using below code but not worked. I/p file BSCBCH1 EXAL-1-4 WO* SMPS MAINS FAIL BSCBCH1 EXAL-1-5 WO* SMPS RECTIFIER FAIL BSCBCH1 EXAL-1-6 WO* SMPS MAJOR ALARM BSCBCH2 EXAL-1-10 WO* ... (5 Replies)
Discussion started by: jaydeep_sadaria
5 Replies

7. Shell Programming and Scripting

Awk to convert a text file to CSV file with some string manipulation

Hi , I have a simple text file with contents as below: 12345678900 971,76 4234560890 22345678900 5971,72 5234560990 32345678900 71,12 6234560190 the new csv-file should be like: Column1;Column2;Column3;Column4;Column5 123456;78900;971,76;423456;0890... (9 Replies)
Discussion started by: FreddyDaKing
9 Replies

8. Shell Programming and Scripting

Removing Carriage return in a file after particular string

Hi All, I want to remove carriage return in a file using some unix command without writing a script my file is as follows abc1 abc2 abc3 abc4 abc5 bac6 abc1 abc2 abc3 abc4 abc5 bac6 I want the output as follows: abc1 abc2 abc3 abc4 abc5 bac6 abc1 abc2 abc3 abc4 abc5 bac6 , Please... (7 Replies)
Discussion started by: manish8484
7 Replies

9. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

10. UNIX for Dummies Questions & Answers

Removing a string of text from a file - help please

Hey Folks, I have a file that contains data that I am working with, sometimes this file has a very long string of text that messes with an awk command in a script i am trying to build. I would like to cut this string of text out of a file and then redirect everything except that string to a new... (5 Replies)
Discussion started by: deepslp
5 Replies
Login or Register to Ask a Question