Cut determinate values amb subst for ; | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Cut determinate values amb subst for ;

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-02-2012
capilla capilla is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 11 June 2013, 4:56 AM EDT
Posts: 16
Thanks: 3
Thanked 0 Times in 0 Posts
Cut determinate values amb subst for ;

Hello
I have a output file that contains the next info:

Code:
host_name   LET-234-WDFD-2
 address    10.11.0.62
 host_name   XWERWE-234-WEDF-EMEEH-GIDF-3
 address    10.11.32.48
 host_name   DFG-745-WE-EMEEEH-GIDF-4
 address    10.11.32.176
        host_name                       DFDFG-ERWG-BL-ETH-01-FV
        address                         198.18.1.107
        host_name                       DFDFG-ERWG-BL-ETH-02-FV
        address                         198.18.1.108
        host_name                       DFDFG-WEFEW-WEFWEF-01-ASDS
        address                         10.11.0.108
 host_name   DFDFG-SDF-UKLY-01-FGG
 address    198.18.253.138
 host_name   DFDFG-SDF-UKLY-02-FGG
 address    198.18.253.139
 host_name   DFDFG-SDF-UKLY-01-GGHTFGH2

The objective is delete host_name & adress an put ; to separate host_name and address:

Code:
LET-234-WDFD-2;10.11.0.62
XWERWE-234-WEDF-EMEEH-GIDF-3;10.11.32.48
DFG-745-WE-EMEEEH-GIDF-4;10.11.32.176
DFDFG-ERWG-BL-ETH-01-FV;198.18.1.107
DFDFG-ERWG-BL-ETH-02-FV;198.18.1.108
DFDFG-WEFEW-WEFWEF-01-ASDS;10.11.0.108
DFDFG-SDF-UKLY-01-FGG;198.18.253.138
DFDFG-SDF-UKLY-02-FGG;198.18.253.139

I try with cut, but any result
Could you help me? Thanks

Last edited by Scrutinizer; 10-03-2012 at 06:02 AM.. Reason: code tags instead of quote tags
Sponsored Links
    #2  
Old 10-02-2012
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Gotham Knight
 
Join Date: Mar 2012
Last Activity: 16 July 2014, 3:22 PM EDT
Location: India
Posts: 1,412
Thanks: 100
Thanked 495 Times in 472 Posts
Assuming a space between the strings and the data,try:

Code:
cut -d' ' -f2- file|paste -sd';\n'

Sponsored Links
    #3  
Old 10-02-2012
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 28 July 2014, 2:16 PM EDT
Location: Foxborough, MA
Posts: 7,644
Thanks: 150
Thanked 577 Times in 542 Posts

Code:
awk 'FNR%2 {print name, $2;next}{name=$2}' OFS=';' myFile

    #4  
Old 10-03-2012
capilla capilla is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 11 June 2013, 4:56 AM EDT
Posts: 16
Thanks: 3
Thanked 0 Times in 0 Posts
Hello Thanks for your reply. I try with the metodes and doesn't work :-(. The result is:

Code:
cut -d' ' -f2- file|paste -sd';\n' extractcritiques.txt
 host_name       cedf-ddt              ;       address         10.14.138.3
        host_name       dfb-crft             ;       address         10.14.184.2
        host_name       cesdneb-cerferu             ;       address         10.14.184.3
        host_name       cenfeeb-derfx;     address         10.14.139.10


Code:
awk 'FNR%2 {print name, $2;next}{name=$2}' OFS=';' extractcritiques.txt | more

;cdceb-adn
10.14.176.6;ceneb-fg
10.14.176.7;ceneb-arerfg
10.14.200.4;ceneb-arrefer
10.14.200.5;ceneb-brfer
10.14.244.96;ceneb-sacrf
10.14.244.97;ceneb-cacrf

I think the problem is the space in to the origen file:

Code:
    host_name       cendfseb-ardfdf
        address         10.14.176.6
        host_name       cesdfb-arsdf
        address         10.14.176.7
        host_name       csgb-atg
        address         10.14.200.4
        host_name       certhrteb-artg

Sorry in my first message don't appear the spaces (or tabulations) because I put in (Wrap quote). awk is very interesting. I'm read the documentation in this moment.

Last edited by Scrutinizer; 10-03-2012 at 06:01 AM.. Reason: code tags instead of quote tags
Sponsored Links
    #5  
Old 10-03-2012
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 27 July 2014, 3:45 PM EDT
Location: Amsterdam
Posts: 9,281
Thanks: 260
Thanked 2,303 Times in 2,066 Posts
Slight mod to vgersh's awk:

Code:
awk 'FNR%2 {name=$2; next} {print name, $2}' OFS=';' infile

alternatively:

Code:
awk '{name=$2; getline; print name, $2}' OFS=';' infile

The Following User Says Thank You to Scrutinizer For This Useful Post:
capilla (10-03-2012)
Sponsored Links
    #6  
Old 10-03-2012
capilla capilla is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 11 June 2013, 4:56 AM EDT
Posts: 16
Thanks: 3
Thanked 0 Times in 0 Posts
Thankss!!!!
When I read your reponse I'm try with:
Quote:
awk 'FNR%2 {name=$2}{print name, $2;next}' OFS=';
You win
Sponsored Links
    #7  
Old 10-03-2012
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 28 July 2014, 1:01 PM EDT
Location: Aachen, Germany
Posts: 3,905
Thanks: 62
Thanked 926 Times in 879 Posts
small corrections to the suggestions will make them work.
1) remove excess spaces using sed:
Code:
sed 's/  */ /g;s/^ //' extractcritiques.txt|cut -d' ' -f2- |paste -sd';\n'

2) invert the result of the %2:
Code:
awk 'FNR%2==0 {print name, $2;next}{name=$2}' OFS=';' extractcritiques.txt

The Following User Says Thank You to RudiC For This Useful Post:
capilla (10-03-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
A simple variable subst is not working IMPe Shell Programming and Scripting 1 06-01-2012 08:49 AM
Compare values in two files. For matching rows print corresponding values from File 1 in File2. Santoshbn Shell Programming and Scripting 10 05-21-2012 11:23 AM
Delete character in determinate position with sed/awk maria_florencia Shell Programming and Scripting 4 01-20-2011 10:01 AM
assign subst|grep|sed command result to a variable snowbiker99 Shell Programming and Scripting 5 11-14-2007 06:08 PM
Command param subst to reg expression videsh77 Shell Programming and Scripting 5 12-21-2004 01:46 PM



All times are GMT -4. The time now is 02:26 PM.