Replacing FQDN by hostnames in a CSV file with sed & awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing FQDN by hostnames in a CSV file with sed & awk
# 1  
Old 01-11-2013
Question Replacing FQDN by hostnames in a CSV file with sed & awk

Hello,

Beginning with shell scipting, I'm trying to find in a csv file, the lines where the field related to hostname is displayed as an FQDN intead the hostname. (some lines are correct) and the to correct that inside the file:
Code:
Novell,11.0,UNIX Server,bscpsiws02,TxffnX1tX1HiDoyBerrzWA==
Red Hat,4.8,,UNIX Server,bscpspas01.bsc.erp,5V2jxrVm09yIU5xgD86RKQ==

using 2 awk with a pipe, I can retrieve the 4th field sparated by comma, and then inside it the first filed separated by the dot.

But how to pass it to Sed to edit the CSV?

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by vbe; 01-11-2013 at 11:06 AM..
# 2  
Old 01-11-2013
Quote:
Originally Posted by Wonto
Hello,

Beginning with shell scipting, I'm trying to find in a csv file, the lines where the field related to hostname is displayed as an FQDN intead the hostname. (some lines are correct) and the to correct that inside the file:
Code:
Novell,11.0,UNIX Server,bscpsiws02,TxffnX1tX1HiDoyBerrzWA==
Red Hat,4.8,,UNIX Server,bscpspas01.bsc.erp,5V2jxrVm09yIU5xgD86RKQ==

using 2 awk with a pipe, I can retrieve the 4th field sparated by comma, and then inside it the first filed separated by the dot.

But how to pass it to Sed to edit the CSV?

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.
Assuming that you mean that you want to change bscpspas01.bsc.erp in the 2nd line of your sample input to bscpspas01 it would be easy to do using:
Code:
awk -F, '$5 ~ /[.]/ {$5 = substr($5, 1, index($5, ".") - 1)}1' OFS="," in

But note that you said you wanted to change an FQDN in field 4 to just the hostname. It looks like you have a hostname in field 4 in the 1st line of your input, but bscpspas01.bsc.erp appears in field 5 in the 2nd line of your input.

Do you want this processing on fields 4 and 5?

Does one of your input lines have an extra comma?

Is one of your lines missing a comma?
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 01-14-2013
Hi Don,

You where right... 2 times!

Your solution if 100 % effective, thanks!
and yes I did a mistake in my example... I trunked a much wide .original csv (around 45 fields for my example, with the host at the 30th position instead of fifth... and did it too quickly (just before leaving my work). My apologizes.

Kind Regards

Cedric
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk and sed script to create one output CSV file

Hi All , I would require your help to generate one output file after post processing of one CSV file as stated below This file is just a small cut from a big file . Big file is having 20000 lines PATTERN,pat0,pat1,pat2,pat3,pat4,pat5,pat6,pat7,pat8,pat9... (2 Replies)
Discussion started by: kshitij
2 Replies

2. Shell Programming and Scripting

How to print & and \n while replacing with sed/awk?

string="din&esh\nisgood" File.txt: the name is sed "s#\#${string}#g" File.txt Output am getting: the name is dinesh is good Expected output: the name is din&esh\nisgood The input string is dynamic it will be keep on changing am able to handle & by placing \& in the string.. (5 Replies)
Discussion started by: dineshaila
5 Replies

3. AIX

Named resolving old fqdn on AIX after change to new fqdn.

Hey All, We have defined abc.this.that in: /etc/named.conf and corresponding files but after the change that we verified through dig -x this.that ptr all is resolving correctly. However in the /var/log/named/named.log file we still see entries for: 4-May-2015 12:15:30.390 queries:... (6 Replies)
Discussion started by: Devyn
6 Replies

4. Shell Programming and Scripting

Combined sed+awk for lookup csv file

have written a combined sed+awk to perform a lookup operation which works but looking to enhance it. looking to match a record using any of the comma separated values + return selected fields from the record - including the field header. so: cat foo make,model,engine,trim,value... (6 Replies)
Discussion started by: jack.bauer
6 Replies

5. Shell Programming and Scripting

Converting txt file into CSV using awk or sed

Hello folks I have a txt file of information about journal articles from different fields. I need to convert this information into a format that is easier for computers to manipulate for some research that I'm doing on how articles are cited. The file has some header information and then details... (8 Replies)
Discussion started by: ksk
8 Replies

6. Shell Programming and Scripting

awk/sed/something else for csv file

Hi, I have a filename.csv in which there are 3 colums, ie: Name ; prefixnumber ; number root ; 020 ; 1234567 user1,2,3 ; 070 ; 7654321 What I want is to merge colum 2 and 3 that it becomes 0201234567 or even better +31201234567 so the country number is used and drop the leading 0.... (9 Replies)
Discussion started by: necron
9 Replies

7. Shell Programming and Scripting

Using awk/sed in handling csv file.

Please study the below script and the output Script: echo "Minimum ${host} ${process} response time=${min} ms" >> ${OUTDIR}/${OUTFILE}; echo "Maximum ${host} ${process} response time=${max} ms" >> ${OUTDIR}/${OUTFILE}; echo "Average ${host} ${process} response time=${avg} ms" >>... (0 Replies)
Discussion started by: ajincoep
0 Replies

8. Shell Programming and Scripting

Need help with a script to process a CSV file using SED and AWK

I get a CSV file every day with 2 columns and multiple rows ex: date1,date2 ( both the fields are varchar fields) This data has to be updated in a table which is being done manually and i want to automate that. 1. I have to select all the data from the prod table( 2 columns { date1,date2}) into... (4 Replies)
Discussion started by: kkb
4 Replies

9. Shell Programming and Scripting

Updating a line in a large csv file, with sed/awk?

I have an extremely large csv file that I need to search the second field, and upon matches update the last field... I can pull the line with awk.. but apparently you cant use awk to directly update the file? So im curious if I can use sed to do this... The good news is the field I want to... (5 Replies)
Discussion started by: trey85stang
5 Replies

10. Shell Programming and Scripting

Help needed - Replacing all date & time occurrences in a file with a string using Sed

Hi, I am new to using Sed. I have a file containg lines like the following: INFORM----Test.pc:168:10/11/05 12:34:26 > some text goes here.. TRACE-----Test.pc:197:10/11/05 12:34:26 > some text goes here.. My requirement is to replace 10/11/05 12:34:26 with a string <RUNDATE> (including <... (4 Replies)
Discussion started by: Hema_M
4 Replies
Login or Register to Ask a Question