Joinging multiple lines that are backslashed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Joinging multiple lines that are backslashed
# 8  
Old 12-22-2009
This should get rid of spaces around the backslash:
Code:
sed ':a;N;s/[ \t]*\\\n[ \t]*/ /;ba' infile

It creates a label a ( a: )
It then joins two lines ( N )
After that leadings space before and after a \ followed by a linefeed (\n) get replaced by a single space
Then it branches (b) to label a.
# 9  
Old 12-22-2009
Quote:
Originally Posted by Scrutinizer
This should get rid of spaces around the backslash:
Code:
sed ':a;N;s/[ \t]*\\\n[ \t]*/ /;ba' infile

It creates a label a ( a: )
It then joins two lines ( N )
After that leadings space before and after a \ followed by a linefeed (\n) get replaced by a single space
Then it branches (b) to label a.
This works great. Thanks :-)

OK, you've given me pretty much exactly what I asked for but this has only highlighted the fact that I mis-stated exactly what I wanted. Ultimately, I'm trying to generate a list of hosts that are represented in the sudoers file. This sed statement (which actually only works if I use gsed on my Solaris 10 host) now leaves me with something like this:

Host_Alias ALIAS1=host1,host2,host3

Host_Alias ALIAS2=host1,host3,host4,host5
Host_Alias ALIAS3=host1,host5,host6

etc.

OK, I can removed blank lines with grep or awk and I can chop off everything up to the = character with another sed statement and I might use tr, for example, to replace all the , characters with a newline character and I'm sure this would all get me what I want.

But ... could it ALL be done in a single command? List every host that's represented in the sudoers file?

- CDM

---------- Post updated at 11:22 AM ---------- Previous update was at 11:11 AM ----------

This is the shortest pipeline I can come up with:

# gsed ':a;N;s/[ \t]*\\\n[ \t]*//;ba;s/*=//g' /etc/sudoers | \
awk -F= '/^Host_Alias/ {print $NF}' | \
tr ',' '\n' | \
sort | \
uniq
# 10  
Old 12-22-2009
Alternatively:
Code:
sed ':a;N;s/[ \t]*\\\n[ \t]*//;ba' /etc/sudoers|sed -n 's/^Host_Alias.*=//;s/,/\n/gp'|sort -u

# 11  
Old 12-22-2009
Quote:
Originally Posted by Scrutinizer
Alternatively:
Code:
sed ':a;N;s/[ \t]*\\\n[ \t]*//;ba' /etc/sudoers|sed -n 's/^Host_Alias.*=//;s/,/\n/gp'|sort -u

This doesn't filter out all the non Host_Alias entries in the file (sorry, that's me not stating the problem correctly again).

What's the significance of the -n and the p after the g?

- CDM
# 12  
Old 12-22-2009
How about this?
Code:
sed ':a;N;s/[ \t]*\\\n[ \t]*//;ba' /etc/sudoers|sed -n '/^Host_Alias/{s/.*=//;s/,/\n/gp}'|sort -u

-n means do not print pattern space automatically
p means print the pattern space
# 13  
Old 12-22-2009
Quote:
Originally Posted by Scrutinizer
How about this?
Code:
sed ':a;N;s/[ \t]*\\\n[ \t]*//;ba' /etc/sudoers|sed -n '/^Host_Alias/{s/.*=//;s/,/\n/gp}'|sort -u

-n means do not print pattern space automatically
p means print the pattern space
Hmmm. OK, this works fine (but, again, only if I use gsed) but it does not capture entries that have only a single host listed like this:

Host_ALias ALIAA8=host12

I suspect because of the assumption of a comma being present on the line?

- CDM
# 14  
Old 12-22-2009
I can do this:
Code:
sed ':a;N;s/[ \t]*\\\n[ \t]*//;ba' /etc/sudoers |
sed '/^Host_Alias/!d;{s/.*=//;s/,/\n/g;s/$/\n/p}'|sort -u

But that does not make it any easier and it prints a blank line

Or this:
Code:
awk -F '[, \t \\\]*' '/Host_Alias/,!/\\/{sub(/^Host_Alias.*=/,"");print}' /etc/sudoers |
grep -o '\w\+' |sort -u

But I don't see how this is an improvement really on what you created yourself (yawn).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing carriage returns from multiple lines in multiple files of different number of columns

Hello Gurus, I have a multiple pipe separated files which have records going over multiple Lines. End of line separator is \n and records going over multiple lines have <CR> as separator. below is example from one file. 1|ABC DEF|100|10 2|PQ RS T|200|20 3| UVWXYZ|300|30 4| GHIJKL|400|40... (7 Replies)
Discussion started by: dJHa
7 Replies

2. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

3. Shell Programming and Scripting

Merging multiple lines to columns with awk, while inserting commas for missing lines

Hello all, I have a large csv file where there are four types of rows I need to merge into one row per person, where there is a column for each possible code / type of row, even if that code/row isn't there for that person. In the csv, a person may be listed from one to four times... (9 Replies)
Discussion started by: RalphNY
9 Replies

4. Shell Programming and Scripting

Reading multiple values from multiple lines and columns and setting them to unique variables.

Hello, I would like to ask for help with csh script. An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Discussion started by: FMMOLA
7 Replies

5. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

6. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

7. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

8. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

9. Shell Programming and Scripting

Logfile parsing with variable, multiple criterias among multiple lines

Hi all I've been working on a bash script parsing through debug/trace files and extracting all lines that relate to some search string. So far, it works pretty well. However, I am challenged by one requirement that is still open. What I want to do: 1) parse through a file and identify all... (3 Replies)
Discussion started by: reminder
3 Replies

10. Shell Programming and Scripting

Find multiple patterns on multiple lines and concatenate output

I'm trying to parse COBOL code to combine variables into one string. I have two variable names that get literals moved into them and I'd like to use sed, awk, or similar to find these lines and combine the variables into the final component. These variable names are always VAR1 and VAR2. For... (8 Replies)
Discussion started by: wilg0005
8 Replies
Login or Register to Ask a Question