Concatenating the two lines in a file


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Concatenating the two lines in a file

hi
My requirement is i have a file with some records like this

file name ::xyz

a=1
b=100,200
,300,400
,500,600
c=700,800
d=900

i want to change my file
a=1
b=100,200,300,400
c=700,800
d=900

if record starts with " , " that line should fallows the previous line.please give script or command to get my required output file

thanks
# 2  
there is always a better solution,

Code:
#! /usr/bin/ksh

awk '{ if( $0 ~ /^,/ ) { printf "%s 1\n",$0 } else { printf "%s 0\n",$0 } }' file | while read
line ind
do
  if [ $ind -eq 0 ]
  then
     echo $var
     var=""
  fi
  var=$var$line
done
echo $var

exit 0

your input file had
,500,600

i believe they should also be appended with the previous lines,

o/p of the above script
Code:

a=1
b=100,200,300,400,500,600
c=700,800
d=900

# 3  
Another way:
Code:
while read LINE
do
echo ${LINE}|grep -qE "^,+"
if [ $? -eq 0 ] 
then
   echo "${LINE}\c"
else
   echo "\n${LINE}\c"
fi
done<input_file
echo

# 4  
Code:
sed -e 'H;$!d;x;s/\n,/,/g' file > newfile

# 5  
Concatenate it

Another solution :

for i in `cat tt`
do
if [ `echo $i | cut -c1-1 ` = "," ]
then
prevline="$prevline$i"
else
echo $prevline
prevline=$i
fi
done
echo $prevline
# 6  
While we're at it, another way:
Code:
#! /bin/bash

while read line; do
 [[ $line == ,* ]] && {
  printf "%s" "$line"
 } || {
  printf "\n%s" "$line"
}
done; printf "\n"

Accepts redirects and input from pipe, as does most of the others. I have to say, I like the sed example above. Pretty nifty...
# 7  
Hi All,

How to combine the two lines of records in a single line using unix shell scripts.

here the file format:
first line
ABC0001 F0000000000677249 677
second line.
ABC00029980000 00000 USA0001099

I would like the output as following
ABC0001 F0000000000677249 677ABC00029980000 00000 USA0001099

need to merge the above two line into a single records.

Please help.

Thanks
ca_sr2274
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Computers
Difficulty: Hard
DHCP stands for Dynamic Host Configuration Port.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Concatenating 2 lines from 2 files having matching strings

Hello All Unix Users, I am still new to Unix, however I am eager to learn it.. I have 2 files, some lines have some matching substrings, I would like to concatenate these lines into one lines, leaving other untouched. Here below is an example for that.. File 1 (fasta file): >292183... (6 Replies)
Discussion started by: Mohamed EL Hadi
6 Replies

2. Shell Programming and Scripting

Concatenating the lines of a data

I have a data of 1 lac lines with the following format abcde,1,2,3,4, ,ee ,ff ,gg ,hh ,mm abcde,3,4,5,6, ,we ,qw ,as ,zx ,cf abcde,1,5,6,7, ,dd ,aa ,er .... .... (6 Replies)
Discussion started by: aravindj80
6 Replies

3. Shell Programming and Scripting

Concatenating more than two lines into one based on some columns

Hi, I need to concatenate some lines in a file based on the First 4 coloumns of a file .. (For Eg.) Consider a file ... I,01,000002,0666,00000.00,000,00,000,000, ,0 I,01,000002,0667,00000.00,000,00,000,000, ,0 I,01,000002,0666,00056.10 I,01,000002,0667,00056.10 I,01,000002,0666,00001... (6 Replies)
Discussion started by: Sri3001
6 Replies

4. Shell Programming and Scripting

Concatenating lines ending with '+' using awk

Hi, I have an ASCII text file where some of the lines are ending with '+' character. I have to concatenate the next successive line with those lines having the trailing '+' char by removing that char. The below awk code has some problems to do this task: awk '{while(sub(/\+$/,"")) {... (12 Replies)
Discussion started by: royalibrahim
12 Replies

5. Shell Programming and Scripting

Concatenating lines in bash

Hi, I'm attempting to join two lines in a file which are separated by a line break. The file contents are shown below: event_id=0 id=0_20100505210853 IFOconfig=HLV template=TaylorF2 Nlive=1000.0 Nruns=1.0 NIFO=3... (7 Replies)
Discussion started by: Supersymmetric
7 Replies

6. Shell Programming and Scripting

awk: switching lines and concatenating lines?

Hello, I have only recently begun with awk and need to write this: I have an input consisting of a couple of letters, a space and a number followed by various other characters: fiRcQ 9( ) klsRo 9( ) pause fiRcQ 9( ) pause klsRo continue 1 aPLnJ 62( ) fiRcQ continue 5 ... and so on I... (7 Replies)
Discussion started by: Borghal
7 Replies

7. Shell Programming and Scripting

Concatenating lines of separate files using awk or sed

For example: File 1: abc def ghi jkl mno pqr File 2: stu vwx yza bcd efg hij klm nop qrs I want the reult to be: abc def ghistu vwx yza jkl mno pqrbcd efg hij klm nop qrs (4 Replies)
Discussion started by: tamahomekarasu
4 Replies

8. Shell Programming and Scripting

Concatenating the lines with different pattern

Hi, I have put a similar question in one of the other threads through which I got the solution shown below but I have some more condition to add to it, hence have further queries on it. I appologies if I should be putting this with the old thread. I have a file which perform a grep on the... (1 Reply)
Discussion started by: simi28
1 Replies

9. Shell Programming and Scripting

concatenating selected lines of multiple files

Hi, I would like a shell script that reads all files in a directory and concatenate them. It is not a simple concatenation. The first few lines of the files should not be included. The lines to be included are the lines from where 'START HERE' appears up to the end of the file. For example, I... (4 Replies)
Discussion started by: laiko
4 Replies

10. Shell Programming and Scripting

Concatenating lines and formatting.

Hi, I have a html file which is unformatted and need to concatenate the lines between each "table" statement in order to run an awk statement on it. Here is the example of the raw file: <table border="0" cellspacing="0" cellpadding="0" class="playerDetails"> def ... (3 Replies)
Discussion started by: Tonka52
3 Replies

Featured Tech Videos