Using find with awk to remove newlines


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using find with awk to remove newlines
# 1  
Old 04-08-2014
Using find with awk to remove newlines

I want to list all html files present in a directory tree, the remove the newline and get one string with a space between files

Code:
find /home/chrisd/Desktop/seg/geohtml/ -name '*.html' | awk BEGIN{FS=\r} '{print}'

---------- Post updated at 06:47 PM ---------- Previous update was at 06:25 PM ----------

Problem is now solved

Code:
find /home/chrisd/Desktop/seg/geohtml/ -name '*.html' | tr -d '\n' | awk '{gsub("/home"," /home"); print}'

# 2  
Old 04-08-2014
If you don't mind having the extra space appear at the end of the list instead of at the start of the list, you can get rid of the call to awk by using:
Code:
find /home/chrisd/Desktop/seg/geohtml/ -name '*.html' | tr '\n' ' '

If the resulting output is more than bytes long, the value returned by the command:
Code:
getconf LINE_MAX

on your system, awk won't work portably. (It may work on some systems, but report a "Line too long" or "Buffer overflow" error, or silently truncate the output on others.)

But more importantly, why do you want to do this at all?
# 3  
Old 04-09-2014
paste is another simple alternative, one which implicitly handles the last line in a manner yielding a single, valid line.

Regards,
Alister
# 4  
Old 04-09-2014
Quote:
Originally Posted by alister
paste is another simple alternative, one which implicitly handles the last line in a manner yielding a single, valid line.

Regards,
Alister
If we go back and look at the original problem statement:
Quote:
I want to list all html files present in a directory tree, the remove the newline and get one string with a space between files
you'll note that a line is not the desired output; a string containing no newlines is what was requested; not a line.

I would still like to know what kristinu wants to do with this string. In 99 and 44/100% of cases where a list of files is needed, I would expect a list of files with one filename per line to be easier to use than a string of space separated filenames (with or without a trailing newline). If any file in the list contains a space character, a space separated string of filenames is useless. (And, yes, I know that it is also possible for a filename to contain a newline which would make a newline separated list of filenames useless.) But in common usage, spaces are much more common in filenames than newlines (thus the 99.44% rather than 100%).
# 5  
Old 04-09-2014
Quote:
Originally Posted by alister
paste is another simple alternative, one which implicitly handles the last line in a manner yielding a single, valid line.

Regards,
Alister
That is
Code:
find /home/chrisd/Desktop/seg/geohtml/ -name '*.html' | paste -s -d" " -

(And there is no extra space at the end.)
# 6  
Old 04-09-2014
Quote:
Originally Posted by Don Cragun
If we go back and look at the original problem statement: you'll note that a line is not the desired output; a string containing no newlines is what was requested; not a line.
According to the verbal description, perhaps, but the tr ... | awk ... solution posted by the OP is an over-complicated and rickety paste ....

Regards,
Alister
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash to remove find and remove specific extension

The bash below executes and does find all the .bam files in each R_2019 folder. However set -x shows that the .bam extension only gets removed from one .bam file in each folder (appears to be the last in each). Why is it not removing the extension from each (this is $SAMPLE)? Thank you :). set... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. UNIX for Beginners Questions & Answers

Remove newlines and carriage return from a csv file using UNIX

I need to remove new lines and carriage returns from csv file. Is there anything other than sed and gwak by which we could achieve this ? Any suggestions ? (3 Replies)
Discussion started by: A_Gaddale
3 Replies

3. UNIX for Dummies Questions & Answers

Remove newlines

Hi buddy's my file are like this: s.no,name,band,sal 1,"suneel",,10 2,"bargav sand",,20 30," ebdug gil",,4 but i want s.no,name,band,sal 1,"suneel",,10 2,"bargav sand",,20 30,"ebdug gil",,4 any command or Shell script for this. please help me it's urgent to implement (33 Replies)
Discussion started by: Suneelbabu.etl
33 Replies

4. Shell Programming and Scripting

Find and Remove

I am trying to get multiple strings removed or replaced with space in a file, for individual strings I have been using the following and it works sed -e 's/#*#*//g' test_1.dat > test_2.dat what needs to be done if multiple strings are to be removed? For example I will need to remove the... (3 Replies)
Discussion started by: btt3165
3 Replies

5. Shell Programming and Scripting

sed remove newlines and spaces

Hi all, i am getting count from oracle 11g by spooling it to a file. Now there are some newline characters and blank spaces i need to remove these. pl provide me a awk/sed solution. the spooled file is attached. i tried this.. but not getting req o/p (6 Replies)
Discussion started by: rishav
6 Replies

6. UNIX for Dummies Questions & Answers

Awk - Handling different types of newlines

Hi. We have some data that's generated from a webpage. Part is pretty well-formatted, but part of it preserves newlines in a way that breaks the record separating in awk. Here's 2 records, filtered through cat -e: Jones,Bob,20,Q: What is your favorite ice cream?$ A: Butter Pecan$ Q: Do you... (6 Replies)
Discussion started by: treesloth
6 Replies

7. Shell Programming and Scripting

find regex and remove #

hi , how do i remove # from a line where i found regex.. don't need to remove all the line.. only remove comment.. (3 Replies)
Discussion started by: Poki
3 Replies

8. Shell Programming and Scripting

Remove improperly placed newlines

Hello, there. I have a file that's a horrible, horrible mess. (Basically, it's an export from a firewall config.) The people who generated the file didn't think that putting a newline in the middle of a hostname would ever be a problem. It is. Here's an example of the stuff in the file: ... (2 Replies)
Discussion started by: mikesimone
2 Replies

9. Shell Programming and Scripting

awk - need to remove unwanted newlines on match

Context: I need to remove unwanted newlines from a data file listing books and associated data. Here is a sample listing ( line numbers included ): 1 360762| Skip-beat! 14 /| 9781421517544| nb | 2008.| Nakamura, Yoshiki.| NAKAMUR | Kyoko Mogami followed 2 her true love Sho to Tokyo to... (6 Replies)
Discussion started by: Bubnoff
6 Replies

10. UNIX for Dummies Questions & Answers

find, remove, and ftp......

I have a script that is run each night by cron. This script generates an extract then places it into an 'extracts' folder and also ftp's a copy to another server. I have set up the script to remove any files in the extracts folder greater than 28 days old (this occurs each time the script is... (1 Reply)
Discussion started by: peter.herlihy
1 Replies
Login or Register to Ask a Question