awk concatenate every line of a file in a single line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk concatenate every line of a file in a single line
# 1  
Old 01-17-2012
awk concatenate every line of a file in a single line

I have several hundreds of tiny files which need to be concatenated into one single line and all those in a single file. Some files have several blank lines. Tried to use this script but failed on it.

Code:
awk 'END { print r } r && !/^/ { print FILENAME, r; r = "" }{ r = r ? r $0 : $0 }' *.txt >concatenated_lines.txt


Last edited by sdf; 01-17-2012 at 05:28 PM..
# 2  
Old 01-17-2012
Code:
# Concatenate many files, strip out newlines to make one line, write to file
cat *.txt | tr -d '\n' > file
# Add final newline to the newline-less line
echo >> file

If you wanted them to be separated by spaces instead of nothing, you could do this:

Code:
cat *.txt | tr -s '\n' ' ' > file
echo >> file

# 3  
Old 01-17-2012
Quote:
Originally Posted by Corona688
Code:
cat *.txt | tr -d '\n' > file
# Add final newline to the newline-less line
echo >> file

If you wanted them to be separated by spaces instead of nothing, you could do this:

Code:
cat *.txt | tr -s '\n' ' ' > file
echo >> file

Would you mind posting the code in awk. I would also need the FILENAME ahead of the concatenated line.
# 4  
Old 01-17-2012
It would've been nice to know if those did what you wanted. If they don't, neither will the same thing written in awk! Smilie

ORS controls the output record separator, which is a newline by default. Setting it blank tells it to print nothing between records, so it'll squeeze it all together. Then at the very end, print one newline, since most things need a newline on the very end to acknowledge it as a line.

Code:
awk -v ORS="" '{$1=$1} 1 END { printf("\n"); }' *.txt > output

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 01-17-2012
Quote:
Originally Posted by Corona688
It would've been nice to know if those did what you wanted. If they don't, neither will the same thing written in awk! Smilie

ORS controls the output record separator, which is a newline by default. Setting it blank tells it to print nothing between records, so it'll squeeze it all together. Then at the very end, print one newline, since most things need a newline on the very end to acknowledge it as a line.

Code:
awk -v ORS="" '{$1=$1} 1 END { printf("\n"); }' *.txt > output

Sorry can't test cat & tr. I am using windowzSmilie

Have tried your code but am still in trouble with the FILENAME in front of each line. I also changed {$1=$1} into {$0=$0} and wondering if this makes it faster?

Code:
awk -v ORS=" " 'BEGIN{ print FILENAME} {$0=$0} 1 ;END { printf("\n"); }' *.txt >output.txt

# 6  
Old 01-17-2012
Quote:
Originally Posted by sdf
Sorry can't test cat & tr. I am using windowzSmilie
You realize these are the UNIX forums, yes? Smilie

Quote:
Have tried your code but am still in trouble with the FILENAME in front of each line. I also changed {$1=$1} into {$0=$0} and wondering if this makes it faster?
Either way is a complete no-op. The only point of it is to inform awk that the data has changed, so that it should translate the newlines into the output record separator of nothing. I'd expect both ways to use nearly no time...

It's always better to show what you want than to post code for a routine which doesn't do what you want; broken code can't be used to show what you do want. Now that I understand your needs a little better:

Code:
awk -v ORS=" " '$1 { print FILENAME, $0; } END { printf("\n"); }' *.txt

Doing an explicit print means not needing to do $1=$1 to get translation. We print exactly what we want instead -- sometimes that's easier than manipulating $0 into what you want printed, and sometimes it's not...

The $1 before the block avoids processing blank lines.

FILENAME is never set in the BEGIN block. BEGIN means the beginning of the program, not the beginning of the line -- and having not yet read any data, there's no FILENAME set inside...
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 01-17-2012
Code:
awk -v ORS=" " '$1 { print FILENAME, $0; } END { printf("\n"); }' *.txt

This code produces the FILENAME for every line in the input File. (at least in gawk).

Quote:
FILENAME is never set in the BEGIN block. BEGIN means the beginning of the program, not the beginning of the line -- and having not yet read any data, there's no FILENAME set inside...
Thanks for the explanation!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Output to file print as single line, not separate line

example of problem: when I echo "$e" >> /home/cogiz/file.txt result prints to file as:AA BB CC I need it to save to file as this:AA BB CC I know it's probably something really simple but any help would be greatly appreciated. Thank You. Cogiz (7 Replies)
Discussion started by: cogiz
7 Replies

2. UNIX for Dummies Questions & Answers

To find and display the middle line in a file using single line command.

Hi all, How can i display the middle line of a file using a single line command? (6 Replies)
Discussion started by: Lakme Pemmaiah
6 Replies

3. Shell Programming and Scripting

Help with concatenate multiple line into one line

Hi, Do anybody experience how to concatenate multiple line into one line by using awk or perl command? Input file: >set1 QAWEQRQ@EWQEASED ASDAEQW QAWEQRQTQ ASRFQWRGWQ From the above Input file, it got 5 lines Desired output file: >set1... (6 Replies)
Discussion started by: perl_beginner
6 Replies

4. Shell Programming and Scripting

Concatenate small line with next line perl script

Hello to all, I'm new to perl, I have input file that contains the string below: 315350535ff450000014534130101ff4500ff45453779ff450ff45545f01ff45ff453245341ff4500000545000This string has as line separator "ff45". So, I want to print each line but the code below is not working. perl -pe '... (2 Replies)
Discussion started by: Ophiuchus
2 Replies

5. Shell Programming and Scripting

Formatting File having big single line into 95 Char Per Line

Hi All, I have 4 big files which contains one big line containing formatted character records, I need to format each file in such way that each File will have 95 Characters per line. Last line of each file will have newline character at end. Before:- File Name:- File1.dat 102 121340560... (10 Replies)
Discussion started by: lancesunny
10 Replies

6. Shell Programming and Scripting

Awk not working due to missing new line character at last line of file

Hi, My awk program is failing. I figured out using command od -c filename that the last line of the file doesnt end with a new line character. Mine is an automated process because of this data is missing. How do i handle this? I want to append new line character at the end of last... (2 Replies)
Discussion started by: pinnacle
2 Replies

7. Shell Programming and Scripting

awk, perl Script for processing a single line text file

I need a script to process a huge single line text file: The sample of the text is: "forward_inline_item": "Inline", "options_region_Australia": "Australia", "server_event_err_msg": "There was an error attempting to save", "Token": "Yes", "family": "Family","pwd_login_tab": "Enter Your... (1 Reply)
Discussion started by: hmsadiq
1 Replies

8. Shell Programming and Scripting

sed / awk to concatenate lines until blank line

Sample input (line feed indicated by ) --------------- The red fox jumped over the brown fence of the red hous He then went into the orchard --------------- Desired Output --------------- The red fox jumped over the brown fence of the red house He then went into the orchard (11 Replies)
Discussion started by: dunstonrocks
11 Replies

9. Shell Programming and Scripting

Concatenate strings line by line

Hi, I have a noob question . Can someone help me how to concatenate line by line using this variables? var1: Apple| Banana| var2: Red Yellow then how can I concatenate both line by line? in which the result would be: Apple|Red Banana|Yellow just to generate a row result i was... (6 Replies)
Discussion started by: hagdanan
6 Replies

10. Shell Programming and Scripting

concatenate all duplicate line in a file.

Hi All, i have a zip file like the format 794051400123|COM|24|0|BD|R|99.98 794051413727|COM|11|0|BD|R|28.99 794051415622|COM|23|0|BD|R|28.99 883929004676|COM|0|0|BD|R|28.99 794051400123|MOM|62|0|BD|R|99.98 794051413727|MOM|4|0|BD|R|28.99 794051415622|MOM|80|0|BD|R|28.99 ... (30 Replies)
Discussion started by: vaskarbasak
30 Replies
Login or Register to Ask a Question