How to print out a newline character in a string in Awk

Richard John Catalano

Tried googling everywhere and I couldn't find anything. I'm trying to figure this out...how would one print out the actual \n char at the end of a line so that in the output file it comes out:

stuff stuff stuff \n stuff stuff stuff \n stuff stuff stuff \n stuff stuff stuff \n

Any and all help is appreciated.

Lix

I believe that all you would have to do is escape the slash in \n in the following way: \\n.

Here is a simple example:

ll | awk '{ printf "%s\\n\n",$1 }'

This should print the permissions column for each file in the current directory and after each line of input, a literal newline character \n is inserted after which an actual newline is inserted.

The result should look something like this:

drwxrwxr-x.\n
-rw-rw-r--.\n
-rw-rw-r--.\n
-rwxrwxr-x.\n
-rwxrwxr-x.\n
drwxrwxr-x.\n
drwxrwxr-x.\n

As you can see, there is a literal \n at the end of each line.


If you don't want any "real" newline characters and have all of your output on one line, you can remove the second \n from the printf statement:

ll | awk '{ printf "%s\\n",$1 }'

This will give you output very similar to what you mentioned in your question with a literal \n appended to each input line:

\n-rwxrwxr-x.\ndrwxrwxr-x.\n-rw-rw-r--.\n-rwxrwxr-x.\ndrwxrwxr-x.\n-rw-rw-r--.\ndrwxrwxr-x.\n-rw-rw-r--.\n

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cannot print newline with newline character in a string

JQ how to print newline and not newline character from json value

how to print quote character in awk

How to recursively print out each character one at a time in a String function

AWK print unexpected newline or end of string inside Shell

How to remove spaces and newline in awk for string insertion?

How to split a java string based on newline character

awk: find multiple string and print out

How to not print <br/> in a string but get a newline instead

How do I print the pipe character in awk

When piping stdout into Perl, how to get it to print a newline character?

How do you print a newline character in BIOS-level assembly?

Awk - how to print variable with string

How to add a character "\t" into a string before the newline character "\n"

entering newline character into a string

JavaScript string newline character?

Trying to figure out how to print a certain string, if the string contains a certain first character

How to Print a string to a specific character?

(Swift) how to print "\" character in a string?

(C Programming) How do I insert string into a character array and then print out all the elements of this array?

Print character by character of a string

How to convert multiline file into a string in bash with newline character?

How to assert that a string has a newline character and, if so, remove it

How to remove the unwanted character like white spaces and newline from a string

Error with awk (newline or end of string)

How to remove space and the specific character in string - awk

How to print an string without the newline Mips Assembly language

How can I print a string with newline without flushing the buffer?

When will awk treat newline character as `;` and when not?