Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Cut determinate values amb subst for ;

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-02-2012
capilla capilla is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 17 March 2015, 10:19 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 Unix and Linux 10-02-2012
elixir_sinari's Unix or Linux Image
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 16 March 2015, 7:53 AM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts
Assuming a space between the strings and the data,try:

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

Sponsored Links
    #3  
Old Unix and Linux 10-02-2012
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is online now Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 3 August 2015, 2:58 PM EDT
Location: Foxborough, MA
Posts: 7,850
Thanks: 175
Thanked 659 Times in 616 Posts

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

    #4  
Old Unix and Linux 10-03-2012
capilla capilla is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 17 March 2015, 10:19 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 Unix and Linux 10-03-2012
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 3 August 2015, 2:25 PM EDT
Location: Amsterdam
Posts: 10,351
Thanks: 351
Thanked 2,748 Times in 2,455 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 Unix and Linux 10-03-2012
capilla capilla is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 17 March 2015, 10:19 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 Linux
Sponsored Links
    #7  
Old Unix and Linux 10-03-2012
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 3 August 2015, 11:38 AM EDT
Location: Aachen, Germany
Posts: 6,411
Thanks: 112
Thanked 1,770 Times in 1,656 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 03:10 PM.