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.

Removing carriage returns with sed

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-19-2006
stevefox stevefox is offline
Registered User
 
Join Date: Aug 2005
Last Activity: 23 August 2011, 3:44 AM EDT
Posts: 122
Thanks: 0
Thanked 0 Times in 0 Posts
Unix or Linux Question Removing carriage returns with sed

How do we delete all carriage returns after a particular string using sed inside a K Shell?

e.g. I have a text file named file1 below:


Code:
$ more file1
Group#=1 User=A
 Role=a1
Group#=2 User=B
 Role=a1
 Role=b1
Group#=3 User=C
 Role=b1

I want the carriage returns to be delete on the line which starts with "Group" to have the below:


Group#=1 User=A Role=a1
Group#=2 User=B Role=a1 Role=b1
Group#=3 User=C Role=b1

Any help will be appreciated.
Sponsored Links
    #2  
Old Unix and Linux 01-19-2006
Manish Jha Manish Jha is offline
Registered User
 
Join Date: Dec 2005
Last Activity: 16 June 2009, 1:18 PM EDT
Location: Boston, USA
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
use \r

use \r to find the carriage return and delete it so that you will get the desired output.
Sponsored Links
    #3  
Old Unix and Linux 01-19-2006
vino's Unix or Linux Image
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 12 March 2015, 5:33 AM EDT
Location: Sydney, Down Under
Posts: 2,848
Thanks: 0
Thanked 13 Times in 13 Posts
Here is non sed solution.

Code:
[/tmp]$ cat replace.ksh 
#! /bin/ksh

SAVE=""

while read line
do

        if [[ "$line" == *Group* ]] ; then

        [[ -n $SAVE ]] && echo "$SAVE" && SAVE=""
        SAVE="$line"

        elif [[ "$line" == *Role* ]] ; then
        SAVE="$SAVE $line"
        fi ;

done < txt

        # For the last accumulated line
        echo "$SAVE"


[/tmp]$ cat txt
Group#=1 User=A
 Role=a1
Group#=2 User=B
 Role=a1
 Role=b1
Group#=3 User=C
 Role=b1
[/tmp]$ ./replace.ksh 
Group#=1 User=A Role=a1
Group#=2 User=B Role=a1 Role=b1
Group#=3 User=C Role=b1

    #4  
Old Unix and Linux 01-19-2006
mahendramahendr mahendramahendr is offline Forum Advisor  
Registered User
 
Join Date: Dec 2005
Last Activity: 22 August 2008, 8:28 AM EDT
Location: London
Posts: 222
Thanks: 0
Thanked 0 Times in 0 Posts
$ nawk '{ if ( $0 ~ /Group/ && NR > 1 ) { printf "\n"; } printf $0; } END { printf "\n"; }' datafile
Group#=1 User=A Role=a1
Group#=2 User=B Role=a1 Role=b1
Group#=3 User=C Role=b1
Sponsored Links
    #5  
Old Unix and Linux 01-20-2006
stevefox stevefox is offline
Registered User
 
Join Date: Aug 2005
Last Activity: 23 August 2011, 3:44 AM EDT
Posts: 122
Thanks: 0
Thanked 0 Times in 0 Posts
Unix or Linux Question

Thanks everyone! I have another question.

I have a text file file1 below:


Code:
$ more file1
Group#=G1
 Role=a1 Role#=[001]
 User=Adam
 User=Ben
 Role=b1 Role#=[002]
 User=Carl
Group#=G2
 Role=a1 Role#=[001]
 User=Ben
 Role=c1 Role#=[003]
 User=Carl
Group#=G3
 Role=c1 Role#=[003]
 User=Adam

...and I want to create a Korn shell that would create the desired output below (Group,Role,User) when I ran the shell using the input file1:


Code:
G1,a1,Adam
G1,a1,Ben
G1,b1,Carl
G2,a1,Ben
G2,c1,Carl
G3,c1,Adam


I first thought of making it one group per line using the awk command below :


Code:
$ awk '{ if ( $0 ~ /Group/ && NR > 1 ) { printf "\n"; } printf $0; } END { printf "\n"; }' file1

Group#=G1  Role=a1 Role#=[001]  User=Adam  User=Ben  Role=b1 Role#=[002]  User=Carl
Group#=G2  Role=a1 Role#=[001]  User=Ben  Role=c1 Role#=[003]  User=Carl
Group#=G3  Role=c1 Role#=[003]  User=Adam

I not sure what to do after thisLinux



Any help will be appreciated.

Last edited by stevefox; 01-20-2006 at 03:27 AM..
Sponsored Links
    #6  
Old Unix and Linux 01-20-2006
vino's Unix or Linux Image
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 12 March 2015, 5:33 AM EDT
Location: Sydney, Down Under
Posts: 2,848
Thanks: 0
Thanked 13 Times in 13 Posts
You need a KSH script or an awk script ?

Here is a ksh script.


Code:
[/tmp]$ cat steve.ksh 
#! /bin/ksh

while read line
do
        if [[ "$line" == *Group* ]] ; then

        GROUP=""
        ROLE=""
        USER=""
        GROUP=${line##*=}

        elif [[ "$line" == *Role* ]] ; then
        USER=""
        ROLE=${line#*=}
        ROLE=${ROLE%% *}

        elif [[ "$line" == *User* ]] ; then
        USER=${line##*=}
        fi ;

        [[ -n $GROUP && -n $ROLE && -n $USER ]] && echo "$GROUP,$ROLE,$USER"
done < text
[/tmp]$ ./steve.ksh 
G1,a1,Adam
G1,a1,Ben
G1,b1,Carl
G2,a1,Ben
G2,c1,Carl
G3,c1,Adam
[/tmp]$

Sponsored Links
    #7  
Old Unix and Linux 01-23-2006
stevefox stevefox is offline
Registered User
 
Join Date: Aug 2005
Last Activity: 23 August 2011, 3:44 AM EDT
Posts: 122
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you very much vino!
I had to make all the double equals signs "==" to a single equals sign "=" to get it work. and it worked great!
By the way could you tell me what the codes below which was in your script is doing?

##*=
%% *
-n

Also if there's a awk solution please tell me if you can.

cheers
Steve
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
Adding carriage returns to file using sed/awk bd_joy Emergency UNIX and Linux Support 2 04-13-2010 11:19 AM
removing carriage returns in text file coolnfunky Shell Programming and Scripting 4 09-16-2009 02:28 PM
Replacing Carriage returns without loosing EOL Majiktom Shell Programming and Scripting 2 03-08-2008 06:28 AM
removing thousand of carriage returns using sed ml5003 Shell Programming and Scripting 4 06-09-2006 05:22 PM
spaces and carriage returns in 'here documents' hcclnoodles Shell Programming and Scripting 0 04-11-2005 06:49 AM



All times are GMT -4. The time now is 05:21 AM.