Newline to space code removes 0 from the first line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Newline to space code removes 0 from the first line
# 1  
Old 12-13-2013
Newline to space code removes 0 from the first line

I am having a peculiar problem. First I run the code below to append 0 at the start of each line in some hundreds of files that I have in a directory. These files have each word in a newline.
Code:
for f in *.dat; do
  echo "0" > tmpfile
  cat $f >> tmpfile
  mv tmpfile $f
done

Then I run this script to remove all the newlines, and convert them into space.
Code:
ls -1 *.dat | while read page
do
cat $page | awk '$1=$1' ORS=' ' $page > $page.txt
done

But I find that when the second code is run, it removes the 0's that I have put using the first code. Any idea how can this be corrected? But when I put any number greater than 0, the second code does not remove that number. I am using Linux with BASH.
# 2  
Old 12-13-2013
It shouldn't be causing a problem, but the cat in
Code:
cat $page | awk '$1=$1' ORS=' ' $page > $page.txt

is redundant - you're passing the filename to awk as a parameter anyway.
This User Gave Thanks to CarloM For This Post:
# 3  
Old 12-13-2013
Yes, "cat" was redundant. It was not needed anyway. But even after removing cat $page in the second script, the same problem of 0 getting removed still persists.
# 4  
Old 12-13-2013
show sample input and expected output.
# 5  
Old 12-13-2013
'$1=$1' will be FALSE if $1 is zero - so it won't print the line.

Try
Code:
 awk '$1=$1 {}1' ORS=' ' $page

EDIT: {}1, not just 1, sorry.
This User Gave Thanks to CarloM For This Post:
# 6  
Old 12-13-2013
Quote:
Originally Posted by CarloM
'$1=$1' will be FALSE if $1 is zero - so it won't print the line.

Try
Code:
 awk '$1=$1 1' ORS=' ' $page

EDIT: {}1, not just 1, sorry.

why don't you just use

Code:
$ awk '1' ORS=' ' file

--edit---

if you use$ awk '$1=$1' file it will eat new line if any, and $1 = $1 1 suffix 1 to field 1

Last edited by Akshay Hegde; 12-13-2013 at 09:35 AM..
This User Gave Thanks to Akshay Hegde For This Post:
# 7  
Old 12-13-2013
Try...
Code:
echo -n "0 " > newfile

...instead.
Note the space...

EDIT:

As you were, ignore my stupidity.
Too many fingers not enough grey matter...

Last edited by wisecracker; 12-13-2013 at 09:34 AM.. Reason: See above...
This User Gave Thanks to wisecracker For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Concatenate integer and space/newline for cout in C++

I'm trying to print out integers and space/newline for a nicer output, for example, every 20 integers in a row with ternary operator. In C I could do it with:printf("%d%s",tmp_int, ((j+1)%20) ? "\t":"\n"); but could not figure out the equivalent in C++: cout << ((j+1)%20)?... (4 Replies)
Discussion started by: yifangt
4 Replies

2. Shell Programming and Scripting

Replace space by newline error

Hello I have had a requirement where I need to move data to a new line based on a text .So basically as soon as it encounters :61: it should move to a new line Source Data : :61:D100,74NCH1 :61:D797,50NCH2 :61:D89,38NCHK2 :61:D99,38NCHK12 :61:D79,38NCHK22 :61:D29,38NCHK5 Target Data... (11 Replies)
Discussion started by: kamijia83
11 Replies

3. UNIX for Dummies Questions & Answers

Replace the unexpected newline char with space in a Fixed width file

Input eg: Ouput Expected. The #rd line had the unexpted new line, which need to be replaced with space. I was planing to go with checking the length of each line using awk and if the length is less than the defeined limit, (12 in above case) will replace the newline with space. ... (5 Replies)
Discussion started by: deepakwins
5 Replies

4. Shell Programming and Scripting

Replace 3rd occurance of SPACE with newline

I have file with SQL output as 0001 firstname1 lastname1 0002 firstname2 lastname2 0003 firstname3 lastname3 0004 firstname4 lastname4 Expected output : 0001 firstname1 lastname1 0002 firstname2 lastname2 0003 firstname3 lastname3 0004 firstname4 lastname4 Let me know if this can... (9 Replies)
Discussion started by: sameermohite
9 Replies

5. Shell Programming and Scripting

Replace newline character between a double quotes to a space

Hi Guys, I have a file with content as below aj.txt "Iam allfine" abcdef abcd "all is not well" What I'm trying to say is my data has some new line characters in between quoted text. I must get ride of the newline character that comes in between the quoted text. output must be:... (8 Replies)
Discussion started by: ajahuja
8 Replies

6. UNIX for Dummies Questions & Answers

Sed to remove only first line erroneously removes last line too

Hello everyone, This is my first posting. I have read the rules of this forum. I have searched many various threads and haven't found one that applies to my situation or suggestions to fix the issue. I do appreciate the help. I am trying to execute a basic UNIX script in a Solaris... (4 Replies)
Discussion started by: dqrgk0
4 Replies

7. UNIX for Dummies Questions & Answers

Remove newline & space

Can someone help me on this. I have a file that has a long line just like below. The long line keeps on being truncated to the next line (new line + space) for some reason. Basically, I just need to remove this problem. Hope somebody can help! Thanks! INPUT FILE: structuralObjectClass:... (4 Replies)
Discussion started by: Orbix
4 Replies

8. Shell Programming and Scripting

Using sed I want to replace space by newline

Input: -------------------------- 123asd 456sdasda 789a ------------------------- output wanted: --------------------- 123asd 456sdasda 789a ---------------------- I want this by sed in simple way please help (I know by: tr ' ' '\n' < inputfile )I want it by sed only (5 Replies)
Discussion started by: RahulJoshi
5 Replies

9. Shell Programming and Scripting

Breaking long lines into (characters, newline, space) groups

Hello, I am currently trying to edit an ldif file. The ldif specification states that a newline followed by a space indicates the subsequent line is a continuation of the line. So, in order to search and replace properly and edit the file, I open the file in textwrangler, search for "\r " and... (14 Replies)
Discussion started by: rowie718
14 Replies

10. UNIX for Advanced & Expert Users

newline character, space and tab after a string

no problem (6 Replies)
Discussion started by: angelina
6 Replies
Login or Register to Ask a Question