AWK: Backslash \ and forcing output not to go onto new lines


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers AWK: Backslash \ and forcing output not to go onto new lines
# 1  
Old 02-05-2010
AWK: Backslash \ and forcing output not to go onto new lines

Dear all,
I am using Mac OSX, have been successfully written an awk script during the last days. I use the script to convert parts of a .dot-file into graphml code.

First question: Backslash
My .dot-code includes repeatedly the sign "\n".

I would like to search for this sign and substitute it.
This does not provide the effect:

Code:
if(labelstring~/:\\n/) { 
                print "Old: " labelstring;  
                sub("\\\n", " ", labelstring);
                print "New: " labelstring;
            }

I get as a result:
Code:
Old: find?:\n2>assumption>contents_vs_form 
New: find?:\n2>assumption>contents_vs_form

Neither does it work if I write
Code:
sub("\\n", " ", labelstring);

.
(Substituting "assumption" works without problems.)

Do you have any hint of how to get the sign "\n" substituted?

Second question: forcing output not to go onto new lines
I wonder if a simple way exists to tell awk to put a number of outputs onto the same line:
awk reads in from asource file a line word by word. i want to print some words, alter some and then print them, and again simply print some together with some additional information of mine ("info").

Code:
do
        {    
            labelword_cleaned = $labelword;
            sub("\",", " ", labelword_cleaned);
            if(labelword_cleaned~/:\\n/) { 
                print "Old: " labelword_cleaned;  sub("\\\n", " ", labelword_cleaned);
                print "New: " labelword_cleaned;
            }
            print labelword_cleaned 
            if($labelword~/\"/) { 
            # This bit of code brings us out of here if we encounter an '"'.
            break};
            ++labelword;
        } while(labelword<80);

This operation acts on up to 80 words of a line. i want the out put (i.e. cleaned up words) to go on one line, without any linebreaks.

Do you have any hints for me?
cheers,
Ingli
# 2  
Old 02-05-2010
Try
Code:
sub("\\\\n", " ", labelstring);

Yes you can alter some words and add your info in awk. Awk reads word based on delimiter supplied to it and holds words in predefined awk variables $1, $2 ,$3... $NF.
# 3  
Old 02-05-2010
question 1 solved, question 2 open

thanks for the immediate reply!

regarding the second question: this is the code of my function (which I call as labeling(2)):
Code:
      function labeling(startlabel_is_n_word) { # This function aims to extract the label of nodes/edges from the .dot file.
        sub("\",", " ", $startlabel_is_n_word);
        print  substr($startlabel_is_n_word, 9) #this prints the string starting with '[label="ActualLabel' as 'ActualLabel'.
        labelword=startlabel_is_n_word+1;
        if($labelword~/\"/) {} else {
        do
        {    
            labelword_cleaned = $labelword;
            sub("\",", " ", labelword_cleaned);
            sub("\\\\n", "\n", labelword_cleaned);
            print labelword_cleaned 
            if($labelword~/\"/) { 
            # This bit of code brings us out of here if we encounter an '"'.
            break}
            ++labelword;
        } while(labelword<80);
        }
    };

The relevant bit here is do {...}.
Thus, I want to apply this bit on each string, starting with $2 and ending with string $80.

My input looks like this:
Code:
    cset0 [label="How are members (not) exact?:\n2>assumption>approximation_improved",

awk transforms this now into
Code:
How
are
members
(not)
exact?:
2>assumption>approximation_improved

How would I need to rewrite this code to have as output:
Code:
How are members (not) exact?: 2>assumption>approximation_improved

Cheers!
# 4  
Old 02-05-2010
Code:
 function labeling(startlabel_is_n_word) { # This function aims to extract the label of nodes/edges from the .dot file.
        sub("\",", " ", $startlabel_is_n_word);
        printf "%s",substr($startlabel_is_n_word, 9) #this prints the string starting with '[label="ActualLabel' as 'ActualLabel'.
        labelword=startlabel_is_n_word+1;
        if($labelword~/\"/) {} else {
        do
        {    
            labelword_cleaned = $labelword;
            sub("\",", " ", labelword_cleaned);
            sub("\\\\n", "\n", labelword_cleaned);
            printf "%s",labelword_cleaned 
            if($labelword~/\"/) { 
            # This bit of code brings us out of here if we encounter an '"'.
            break}
            ++labelword;
        } while(labelword<80);
        }
        printf "\n"
    };

# 5  
Old 02-05-2010
closed

thousand thanks! works wonderfully!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to retain header lines in output

The awk below executes and produces the current output, which is correct, except I can not seem to include the header lines # and ## in the output as well. I tried adding !/^#/ thinking that it would skip the lines with # and output them but the entire file prints as is. Thank you :). file ... (8 Replies)
Discussion started by: cmccabe
8 Replies

2. Shell Programming and Scripting

awk to add lines with symbol to output file

In the awk below which does execute I get output that is close, except for all the lines that start with a # are removed. Some lines have one others two or three and after the script adds the ID= to the fields below the pattern in the awk, I can not seem to add the # lines back to the output. ... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Remove lines from output in files using awk

I have two large files (~250GB) that I am trying to remove the where GT: 0/0 or 1/1 or 2/2 for both files. I was going to use a bash with the below awk, which I think will find each line but how do I remove that line is that condition is found? Thank you :). Input 20 60055 . A ... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. Shell Programming and Scripting

awk to output lines less than number

I am trying to output all lines in a file where $7 is less than 30. The below code does create a result file, but with all lines in the original file. The original file is tab deliminated is that the problem? Thank you :). awk 'BEGIN{FS=OFS=","} $7 < 30 {print}' file.txt > result.txt... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

How can I get sed to include backslash and 'f' in the output

Both of these fail. One has two form feeds, the second form leaves all the backslashes. bold='\(code\|command\|var\|samp\|option\|strong\)' sed -e "s;@${bold}{"'\(*\)};\fB\2\fP;g' sed -e "s;@${bold}{"'\(*\)};\\fB\2\\fP;g' Obviously, I'm trying to change texi markup into man page markup, but it... (3 Replies)
Discussion started by: bkorb
3 Replies

6. Shell Programming and Scripting

awk file comparison, x lines after matching as output

Hello, I couldn't find anything on the Forum that would help me to solve this problem. Could any body help me process below data using awk? I have got two files: file1: Worker1: Thomas Position: Manager Department: Sales Salary: $5,000 Worker2: Jason Position: ... (5 Replies)
Discussion started by: killerbee
5 Replies

7. Shell Programming and Scripting

Print duplicate only lines as normal output - Awk

input output a1 100 200 XYZ_X a1 98 188 ABC (2 Replies)
Discussion started by: quincyjones
2 Replies

8. Shell Programming and Scripting

Need to extract some lines from output via AWK

Hello Friends, I have got, this output below and i want to extract the name of symlink which is highlighted in red and the path above it highlighted in blue. At the end i want to append path and symlink. /var/tmp/asirohi/jdk/jre /var/tmp/asirohi/jdk/jre/.systemPrefs... (3 Replies)
Discussion started by: asirohi
3 Replies

9. Shell Programming and Scripting

Merge lines in a file with Awk - incorrect output

Hi, I would like: FastEthernet0/0 is up, line protocol is up 0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored 0 output errors, 0 collisions, 0 interface resets Serial1/0:0 is up, line protocol is up 0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort 0... (14 Replies)
Discussion started by: mv652
14 Replies

10. Shell Programming and Scripting

Sed and awk backslash characters

Hi, I have a variable read from user input: PROFILESROOTDIR="\\194.185.82.188\CMSRepository\EncodingProfiles" awk -F"=" -v gr=$PROFILESROOTDIR '/ProfilesRootDirectoryFromXOEMachine/{$2=gr;}1' OFS="=" $CFGFILE > "${CFGFILE}_new" For this awk to work properly I need to replace in the... (7 Replies)
Discussion started by: potro
7 Replies
Login or Register to Ask a Question