Please Help! Need to put the lines of a txt to one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please Help! Need to put the lines of a txt to one line
# 1  
Old 11-17-2012
Please Help! Need to put the lines of a txt to one line

Hi all, I'm quite newbie in shell scripting but I found a problem what I cant solve.
I have a .txt file which looks like this:
/22/
/23/
/24/
and so on and I'd need to make it look like this:
/22/|/23/|/24/|...and so on.

these numbers are growing and has lines like this /2a/ as well.
Please help me Smilie(
# 2  
Old 11-17-2012
try

Code:
 paste -sd"|" file

# 3  
Old 11-17-2012
and how should I continue? coz it gives back only the first line.
# 4  
Old 11-17-2012
Here are two ways to do this with awk:
Code:
awk 'FNR == 1 { o = $0; next}
{       o = o "|" $0}
END {   print o}' in.txt

Code:
awk '{  printf("%s%s", FNR == 1 ? "" : "|", $0)}
END {   printf("\n")}' in.txt

The second of these will always work as long as all of the input lines are shorter than {LINE_MAX} on your system. The first will reliably work as long as the accumulated output line doesn't exceed {LINE_MAX} on your system.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 11-17-2012
Another awk, with trailing "|" like in the sample:
Code:
awk 'END{print RS}1' ORS=\| infile

--
Otherwise:
Code:
awk 'NR>1{print "|"} END{print RS}1' ORS= infile

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 11-17-2012
I havent heard about awk yet, here the in.txt is the file which contain the datas or it will be the one which gets the new datas?
Can I use it with a .sh file? Is it ok if I just put this code right under #/bin/sh ?
And what parameter do I need to give when I'd like to run it? The file which contains the datas or the one which will contain and where should I give the value of line max?
Sorry for these questions I'm a noob.
# 7  
Old 11-17-2012
Quote:
Originally Posted by gergo235
and how should I continue? coz it gives back only the first line.
That is for you to specify. How wide should the lines be?

--
Quote:
Originally Posted by gergo235
I havent heard about awk yet, here the in.txt is the file which contain the datas or it will be the one which gets the new datas?
Can I use it with a .sh file? Is it ok if I just put this code right under #/bin/sh ?
And what parameter do I need to give when I'd like to run it? The file which contains the datas or the one which will contain and where should I give the value of line max?
Sorry for these questions I'm a noob.
1. the file that contains the data.
2. You could do that..
3. Yes
4. None
5. You cannot set the value of LINE_MAX
This User 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

Bash incert line from 1.txt to 2.txt

i would like to insert a line from 2.txt into 1.txt between " and " or a way of adding to the end of each line " _01_ and have the numbers correspond to the line # 1.txt= foofoo "" _01_ foofoo "" _02_ foofoo "" _03_ foofoo "" _04_ 2.txt= ... (6 Replies)
Discussion started by: klein
6 Replies

2. Shell Programming and Scripting

Put in one line only the lines with digits

Hello, I haven't been here for a while and I might be forgetting some things. I have an input text like this titleA http://myurl/bla/blabla/1234 http://myurl/bla/blabla/6789 titleB http://myurl/bla/blabla/5678 http://myurl/bla/blabla/1234 titleC http://myurl/bla/blabla/9123... (10 Replies)
Discussion started by: Kibou
10 Replies

3. UNIX for Dummies Questions & Answers

Split Every Line In Txt Into Separate Txt File, Named Same As The Line

Hi All Is there a way to export every line into new txt file where by the title of each txt output are same as the line ? I have this txt files containing names: Kandra Vanhooser Rhona Menefee Reynaldo Hutt Houston Rafferty Charmaine Lord Albertine Poucher Juana Maes Mitch Lobel... (2 Replies)
Discussion started by: Nexeu
2 Replies

4. Shell Programming and Scripting

Need to append the date | abcddate.txt to the first line of my txt file

I want to add/append the info in the following format to my.txt file. 20130702|abcd20130702.txt FN|SN|DOB I tried the below script but it throws me some exceptions. <#!/bin/sh dt = date '+%y%m%d'members; echo $dt+|+members+$dt; /usr/bin/awk -f BEGIN { FS="|"; OFS="|"; } { print... (6 Replies)
Discussion started by: harik1982
6 Replies

5. Shell Programming and Scripting

how to read strings from a txt and put them into a spreadsheet?

i have hundreds of thousands of txt files as below, RADARSAT 1 SCENE DESCRIPTION SCENE_ID c0005098 MDA ORDER NUMBER GEOGRAPHICAL AREA CIS ScanSar Canada SCENE START TIME APR 02 1997 23:05:10.222 SCENE STOP TIME APR 02 1997 23:02:49.695... (5 Replies)
Discussion started by: sunnydanniel
5 Replies

6. UNIX for Dummies Questions & Answers

find lines in file1.txt not found in file2.txt memory problem

I have a diff command that does what I want but when comparing large text/log files, it uses up all the memory I have (sometimes over 8gig of memory) diff file1.txt file2.txt | grep '^<'| awk '{$1="";print $0}' | sed 's/^ *//' Is there a better more efficient way to find the lines in one file... (5 Replies)
Discussion started by: raptor25
5 Replies

7. Shell Programming and Scripting

sed to cp lines x->y from 1.txt into lines a->b in file2.txt

I have one base file, and multiple target files-- each have uniform line structure so no need to use grep to find things-- can just define sections by line number. My question is quite simple-- can I use sed to copy a defined block of lines (say lines 5-10) from filename1.txt to overwrite an... (3 Replies)
Discussion started by: czar21
3 Replies

8. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

9. Shell Programming and Scripting

using tr to put multiple lines of output into one line

Hi all, For a intro UNIX course I'm taking, I need to use the command "tr" to display a file on standard output without any newlines (all on one line). I assume I would start with "cat filename | tr" but don't know what to put after tr. Any ideas would be lovely! Thanks. (3 Replies)
Discussion started by: otes4
3 Replies

10. Shell Programming and Scripting

read a part of information from txt and put into the script

Hi, I have a name.txt which is stored: APPLE ORANGE RED BLUE GREEN and my script is: $name=`cat name.txt for file_number in `ls 1 /appl/CH_DATA/archive/CACHE/CDBACKUP$name*.archived however, my script cannot read name.txt and put into my scrip line, I would like the output is to... (18 Replies)
Discussion started by: happyv
18 Replies
Login or Register to Ask a Question