awk - make new line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - make new line
# 1  
Old 10-03-2013
awk - make new line

Hi,

I would like to make a new line example like below:

"
Code:
When you post an item you can just delete the files.

"

every 3 words of the above sentence I would like to make a new line like below:
"
Code:
When you post
an item you
can just delete
the files

"
and after the last line "the files", I would like to make an empty line.

Any advise/suggest would greatly appreciate.

Thanks

Last edited by Scrutinizer; 10-04-2013 at 08:05 AM.. Reason: code tags
# 2  
Old 10-03-2013
Try:
Code:
STRING="When you post an item you can just delete the files."
WORDS=0
for WORD in $STRING;do
    printf "$WORD "
    let WORDS++ ; [[ $WORDS -gt 2 ]] && WORDS=0
    [[ $WORDS -eq 0 ]] && printf "\n"
done
printf "\n"

EDIT: Sorry just figured you're refering to awk, which i dont know either.

Last edited by sea; 10-03-2013 at 11:36 PM.. Reason: codefix ;)
# 3  
Old 10-03-2013
Code:
str="When you post an item you can just delete the files."

echo $str  | awk '
	{	for ( i=1; i<=NF; i++ ) { 
			printf $i FS; 
			if ( i%3 == 0 )
				printf "\n"
		}
	} END { printf "\n" }
'

# 4  
Old 10-03-2013
Hello jethrow, what is NF and FS you're using in the syntax?
# 5  
Old 10-04-2013
Check out the quick reference guide here: An Awk Primer
# 6  
Old 10-04-2013
An awk variation without loop
Code:
awk '{printf "%s" (NR%3?" ":"\n"),$0} END {print ""}' RS=" " file
When you post
an item you
can just delete
the files.

# 7  
Old 10-04-2013
If you can also use something other than awk, try:
Code:
xargs -n 3 < file

-or-

Code:
$ var="When you post an item you can just delete the files."
$ printf "%s %s %s\n" $var
When you post
an item you
can just delete
the files.

These 2 Users Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to make a new line of the file?

Hi, I have a file (newfile.txt) with the contents below: This is a new file. This is a new file. This is a new file. This is a new file. How could I make a new line of the file as below? This is a new file. This is a new file. This is a new file. This is a new file. When there is a... (12 Replies)
Discussion started by: khchong
12 Replies

2. Shell Programming and Scripting

Make Multile line is one line using window Perl

Hi All I have a .csv file which is showing data as ESP Client,ESP Engagement,Misc_Projects_120101,DEFAULT,HA,Unknown,No,Unknown,201704,4.1,Unknown,AAA,Collected-Done,"she,joy.",200111,Unknown,Full Time,,Delivery_DONE AMO,Approved,2012-12-03,2012-12-06,2012-12-06,"Occupied Hours ... (7 Replies)
Discussion started by: adisky123
7 Replies

3. Shell Programming and Scripting

search between keywords and make a single line

have a very big file where need to format it like below example file: abcd today is great day; search keyword 'abcd' and append to it all words till we reach ; to make it a single line. output should look like. abcd today is great day; There are many occurrence of such... (2 Replies)
Discussion started by: giri4332
2 Replies

4. UNIX for Dummies Questions & Answers

search all file for particular text and make changes to line 3

Hi All, I am sitting on HPUX. I want to change the exit into #exit, which appears into 3red line of code in shell scripting, wondering how shell script to be called up to perform action. I have following code in all files. Now, I need to find the text exit and replace into #exit. #!/sbin/sh... (10 Replies)
Discussion started by: alok.behria
10 Replies

5. Shell Programming and Scripting

How to make command line interactive?

I have a file with stats for different month, I use the cat and grep combination to search for different month. cat <file> | grep "April" cat <file> | grep "May" cat <file> | grep "June" etc. How do you make this command interactive. So that I can give the values for the search. Thanks... (7 Replies)
Discussion started by: purelltab
7 Replies

6. Shell Programming and Scripting

Try to make a single line to syslog.

Hi again: Following the thread: https://www.unix.com/shell-programming-scripting/147755-format-lines-file.html I couldn't solve my problem, so I ask you again gurus: I have this code: nohup /usr/sbin/auditstream | /usr/sbin/auditselect -m -e "event== S_ENVIRON_WRITE || event==... (2 Replies)
Discussion started by: iga3725
2 Replies

7. Shell Programming and Scripting

make multiple line containing a pattern into single line

I have the following data file. zz=aa azxc-1234 aa=aa zz=bb azxc-1234 bb=bb zz=cc azxc-1234 cc=cc zz=dd azxc-2345 dd=dd zz=ee azxc-2345 ee=ee zz=ff azxc-3456 ff=ff zz=gg azxc-4567 gg=gg zz=hh azxc-4567 hh=hh zz=ii azxc-4567 ii=ii I want to make 2nd field pattern matching multiple lines... (13 Replies)
Discussion started by: VTAWKVT
13 Replies

8. Linux

how to enable #ifdef macro in the command line of make?

Linux, C++, make, macro In source code, we used some #ifdef macros. How can I enable #ifdef macro in the command line of "make" (NOTE: I do NOT want to change source code or makefile to define that macro from time to time). e.g. test.cpp: ... #ifdef TEST1 // code segment for test1 ...... (3 Replies)
Discussion started by: princelinux
3 Replies

9. Shell Programming and Scripting

to make 2 lines into 1 line using awk

suppose i have a file AAAHHHHAHH AJJJJAAAAAA AJJJJJAAJJAK AJJAJJAJKKK the output suhd be AAAHHHHAHHAJJJJAAAAAAAJJJJJAAJJAKAJJAJJAJKKK (2 Replies)
Discussion started by: cdfd123
2 Replies

10. UNIX for Dummies Questions & Answers

How to make all lines into 1 line?

I have a file listing IP addresses, 1 per line, such as: 1.2.3.4 3.4.5.6 12.13.14.15 7.8.9.6 I want all of the entries to be on the same line, and quoted, such as: "1.2.3.4" "3.4.5.6" "12.13.14.15" "7.8.9.6" I got the quotes on there in vi with ":%s/^/"/g" and "%s/$/"/g" ... is there... (8 Replies)
Discussion started by: earnstaf
8 Replies
Login or Register to Ask a Question