Get extract and replace column with link in a column where it exists


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get extract and replace column with link in a column where it exists
# 1  
Old 10-26-2017
Get extract and replace column with link in a column where it exists

hi i have sample data


Code:
a,b,c,d,e,g h http://mysite.xyx
z,b,d,f,e,s t http://123124#
a,b,c,i,m,nothing
d,i,j,e,w,nothing

output expected is
Code:
a,b,c,d,e,http://mysite.xyx
z,b,d,f,e,http://123124#
a,b,c,i,m,nothing
d,i,j,e,w,nothing

i can get only links using grep -o 'http.*'

i tried something like below it doesn't work

Code:
for i in `cat file.csv`
do
first=$i|awk '{print $1}'
second=$i|awk '{print $2}'
third=$i|awk '{print $3}'
four=$i|awk '{print $4}'
five=$i|awk '{print $5}'
six=$i|awk '{print $6}'
 if [ $six = "nothing" ] 
 then
 six=$six
 else
    six=`grep -o 'http.*' $six`
 fi
echo "$first,$second,$third,$four,$five,$six"
 done >> output.csv


Last edited by zozoo; 10-26-2017 at 11:43 AM.. Reason: corrected the output expected
# 2  
Old 10-26-2017
Hello zozoo,

There are lot of questions in output shown by you.
I- By what logic you have removed lines a,b,c,i,m,nothing and d,i,j,e,w,nothing?
II- By which logic line z,b,d,f,e,s t http://123124# changed to z,b,d,f,e,http://mysite.xyx?

Would like to request you to please be clear in your posts.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 10-26-2017
Quote:
Originally Posted by RavinderSingh13
Hello zozoo,

There are lot of questions in output shown by you.
I- By what logic you have removed lines a,b,c,i,m,nothing and d,i,j,e,w,nothing?
II- By which logic line z,b,d,f,e,s t http://123124# changed to z,b,d,f,e,http://mysite.xyx?

Would like to request you to please be clear in your posts.

Thanks,
R. Singh
hi Ravinder i am sorry for the wrong output corrected now
so basically i want to check if the sixth column is having any url then replace the field with url else leave it what ever value it is having .
# 4  
Old 10-26-2017
Quote:
Originally Posted by zozoo
hi Ravinder i am sorry for the wrong output corrected now
so basically i want to check if the sixth column is having any url then replace the field with url else leave it what ever value it is having .
Hello zozoo,

Could you please try following and let me know if this helps you.
Code:
awk -F',| ' '{print $1,$2,$3,$4,$5,$NF}' OFS=,   Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 10-26-2017
Another way:
Code:
awk 'NF>1{sub(/[^,]*$/,$NF)}1' file

or
Code:
sed 's/[^,]* //' file


--
(same thing with awk: )
Code:
awk '{sub(/[^,]* /,x)}1' file

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 10-26-2017
Quote:
Originally Posted by RavinderSingh13
Hello zozoo,

Could you please try following and let me know if this helps you.
Code:
awk -F',| ' '{print $1,$2,$3,$4,$5,$NF}' OFS=,   Input_file

Thanks,
R. Singh
That solved i was trying to another version just now to match the http string and then split by space into array to retrun the value , but your solution solved it

so in the solution you are trying to split by delimeter , or <space> right and $NF would give last field am i correct in understanding the solution

---------- Post updated at 08:50 PM ---------- Previous update was at 08:41 PM ----------

Quote:
Originally Posted by Scrutinizer
Another way:
Code:
awk 'NF>1{sub(/[^,]*$/,$NF)}1' file

or
Code:
sed 's/[^,]* //' file

--
(same thing with awk: )
Code:
awk '{sub(/[^,]* /,x)}1' file

Hi Scrutinizer the solution you provided also works its difficult to understand can you please explain
# 7  
Old 10-26-2017
Quote:
Originally Posted by zozoo
[..]
Hi Scrutinizer the solution you provided also works its difficult to understand can you please explain
Hi zozoo,

The first approach replaces the part after the last comma with the last field ($NF)
The other ones remove the part after the last comma upto and including the last space

[^,] means "a character that is not a comma".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract a column and multiple by 1000 and replace it on same file

Hi All, I need to extract a position in the file and multiple the value by 1000 and the replace it . Original 0010001200084701217637306521200000000000010010000000 ---> 000847 * 1000 0010012700086001213437404323000000000000001001000000 ---> 000860 * 1000... (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

2. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

3. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

4. Shell Programming and Scripting

Need to extract data from Column having variable length column

Hi , I need to extract data from below mentioned data, having no delimiter and havin no fixed column length. For example: Member nbr Ref no date 10000 1000 10202012 200000 2000 11202012 Output: to update DB with memeber nbr on basis of ref no. ... (6 Replies)
Discussion started by: ns64110
6 Replies

5. Shell Programming and Scripting

for each different entry in column 1 extract maximum values from column 2 in unix/awk

Hello, I have 2 columns (1st column has multiple entries but the corresponding values in the column 2 may be the same or different.) however I want to extract unique values for each entry in column 1 by assigning the max value from column 2 SDF4 -0.211654 SDF4 0.978068 ... (1 Reply)
Discussion started by: Diya123
1 Replies

6. Shell Programming and Scripting

Replace column with column from another file

Hello, I am trying to replace the column in file1 with the column from file2. The two files will have the same amount of rows. Each row will correspond with the same row in the other file. File1 "Replace this column" 500 13-APR-2011... (11 Replies)
Discussion started by: doobe01
11 Replies

7. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

8. Shell Programming and Scripting

awk/sed column replace using column header - help

$ cat log.txt Name Age Sex Lcation nfld alias xsd CC 25 M XYZ asx KK Y BB 21 F XAS awe SS N SD 21 M AQW rty SD A How can I replace the column with header "Lcation" with the column with header "alias" and delete the "alias" column? so that the final output will become: Name Age Sex... (10 Replies)
Discussion started by: jkl_jkl
10 Replies

9. UNIX for Advanced & Expert Users

replace a column values with the first value in column

Hi All, I have a file which has data in following format: "Body_Model","2/1/2007","2/1/2007" "CSCH74","0","61" "CSCS74","0","647" "CSCX74","0","3" "CSYH74","0","299" "CSYS74","0","2514" "CSYX74","0","3" "Body_Model","3/1/2007","3/1/2007" "CSCH74","0","88" "CSCS74","0","489"... (3 Replies)
Discussion started by: sumeet
3 Replies

10. Shell Programming and Scripting

Replace 10th column with a new column--- Terriblly hurry

Hi Can any one tell me how to replace the 10th column in a file(comma delimted) with a new file with a single column. Can any one Help me out of the please as soon as possible as i am in a terribley hurry!!!!!! Many THanks, (2 Replies)
Discussion started by: ahmedwaseem2000
2 Replies
Login or Register to Ask a Question