The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Join Every 5 Lines With $ Symbol ntgobinath Shell Programming and Scripting 3 05-08-2008 10:01 AM
to join the corresponding lines using shell commands or awk cdfd123 Shell Programming and Scripting 7 12-10-2007 04:45 PM
Awk Join multiple lines hitmansilentass Shell Programming and Scripting 5 10-27-2006 11:04 AM
join two lines when the second line contains "US DOLLAR" powah Shell Programming and Scripting 2 10-21-2005 06:30 PM
join two lines together tine Shell Programming and Scripting 4 12-12-2003 12:34 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-11-2008
glamo_2312 glamo_2312 is offline
Registered User
  
 

Join Date: Sep 2008
Location: INDIA
Posts: 19
how to join lines

can anyone tell me as "how to join all lines in a file " using a shell script
Actually i have many files in a directory and for each file i want to join all the lines using a shell scrip .

Thanks in advance!!!
  #2 (permalink)  
Old 09-11-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,720
This joins a list of files into one line per file in a new file
Code:

while read file
do
      tr -d '\n' < $file
      echo ""
done < list_of_files > newfile
  #3 (permalink)  
Old 09-11-2008
Niroj Niroj is offline
Registered User
  
 

Join Date: Aug 2008
Location: Bangalore, INDIA
Posts: 55
echo " Enter the dir name: [Give full path] "
read dir
cd $dir

for file in `ls`
do
if [ -f $file ]; then # Plz try to check & avoid *.gz , *.out & *.4ge
# and other binary files
echo `cat $file`> $file
fi
done

Last edited by Niroj; 09-11-2008 at 08:09 AM..
  #4 (permalink)  
Old 09-11-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
That's a Useless Use of ls in Backticks there. You mean

Code:
for file in *
That's also a Useless Use of Cat; or rather, it will replace runs of newlines with a single space, which is kind of borderline; certainly the lines are joines, but spaces are also added. If that's not desired, try

Code:
tr -d '\n' <"$file" >tmp;  mv tmp "$file"
(If your tr doesn't understand \n, try with \012 or a literal newline between the single quotes. It looks weird but it's perfectly valid syntax.)

If you want to replace each newline with a space, that's also easy to do with tr.
  #5 (permalink)  
Old 09-11-2008
Niroj Niroj is offline
Registered User
  
 

Join Date: Aug 2008
Location: Bangalore, INDIA
Posts: 55
ok...Herder of Useless Cats..
cool... I knew the result thats why I suggested this...
Ex.
cat > File
=============
hi era
bye
=========
tr -d '\n' <"File" >tmp; mv tmp "File"
=====
cat file
----------------
hi erabye # Any one need this type of joining.. ??!

================================================
And 2nd one ...
If you think "translating each '\n' char to NULL value; then creating a temp file and then renaming the temp file to acutual file name to make it permanent .. " is faster and better idea..

Then I don't need to say anything...!!

Last edited by Niroj; 09-11-2008 at 09:12 AM..
  #6 (permalink)  
Old 09-12-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Depends on the requirements, obviously. If you want spaces then (as hinted) try

Code:
tr '\n' ' ' <File >tmp
Temporary files are obviously a wart, but if this is part of a complex process, the command is probably part of a longer pipeline anyway, and so no temporary file will be needed. If you want a one-liner without a temporary file, try Perl.

Code:
perl -i~ -pe 'y/\n//d' file  # or 'y/\n/ /' if you want to join with spaces
  #7 (permalink)  
Old 09-12-2008
Niroj Niroj is offline
Registered User
  
 

Join Date: Aug 2008
Location: Bangalore, INDIA
Posts: 55
Quote:
Originally Posted by era View Post
Depends on the requirements, obviously. If you want spaces then (as hinted) try

Code:
tr '\n' ' ' <File >tmp
Temporary files are obviously a wart, but if this is part of a complex process, the command is probably part of a longer pipeline anyway, and so no temporary file will be needed. If you want a one-liner without a temporary file, try Perl.

Code:
perl -i~ -pe 'y/\n//d' file  # or 'y/\n/ /' if you want to join with spaces
=======================
What a greate idea..as if anyone know these...?!!
Plese don't make thse silly comments..

Then tell me why using cat is useless...

echo `cat $file`> $file ?



Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:39 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0