LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to write line1 line2 side by side (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-write-line1-line2-side-by-side-824937/)

secondchanti 08-08-2010 12:09 PM

How to write line1 line2 side by side
 
Friends,
I am having a text file as below ;


line 1 : 123 rama 21
line 2 : dropped INDIA Remarks

i want out put as below :

line 1 line 2 ie

123 rama 21 dropped INDIA Remarks ( in one line)


which command i can use ?

Please guide me.

Rao

TB0ne 08-08-2010 12:18 PM

Quote:

Originally Posted by secondchanti (Post 4059920)
Friends,
I am having a text file as below ;

line 1 : 123 rama 21
line 2 : dropped INDIA Remarks

i want out put as below :
line 1 line 2 ie
123 rama 21 dropped INDIA Remarks ( in one line)

which command i can use ?
Please guide me.

We can "guide you" to read some of your other threads, where you're doing very similar things to this. You can use "tr" or "sed" to do it.

PTrenholme 08-08-2010 12:36 PM

Try echo -n line1; echo " " line2 or simply echo line1 line2 if you don't need to write them at different times.

<edit>
Here's a script that reads an input file and writes the consecutive lines next to each other:
Code:

#!/bin/bash
i=0
while read
do
  i=$((${i}+1))
  if [ $((${i}%2)) -eq 0 ]
  then
    hold="${REPLY}"
  else
    echo ${hold} ${REPLY}
  fi
done < $1
[ $((${i}%2)) -eq 0 ] && echo ${hold}

You should be able to add any additional processing you need to that script.
</edit>

secondchanti 08-08-2010 09:17 PM

Dear PTrenholme

my file name is "FILE1" and my reuquired output file is "OUTPUTFILE". and my inputfile contains around 1000 lines. Where i have to mention input file and output file names in the shall programme.

pl guide
RAo

PTrenholme 08-09-2010 05:44 PM

In the next-to-last line of the script, done < $1, the input to the read is taken from the first argument passed to the script. The output goes to the standard output by default.

So, if you were to take the script I posted and save it to a file, say, for example, "squash" then you would do the following:
  1. Make squash executable: chmod +x squash
  2. Run it like this: ./squash FILE1 > OUTPUT

Oops! :redface: I was just looking at the code I wrote, and saw that the modulus test ($((${i}%2) should have been 1, not 0.

Here's a fancier version of the code with some sanity checks, and changes so you can specify both the input and output file names on the command line. Note that I've only done a few, brief, tests, and cannot warrant that it will satisfy your needs. (I.e., use at your own risk.)
Code:

#!/bin/bash
# Function to print help and exit
usage() {
if [ $# -eq 0 ]
then
  cat <<EOF >/dev/stderr
$0: Merge alternate lines into single line.

  Usage: $0 Input_file Output_file
EOF
  exit ${1}
fi
}
#
# Sanity checks
#
# Two arguments required
[ $# -ne 2 ] && usage 1
# Input file set to stdin if null. Must exist and be readable
in="${1}"
[ "${in}" == "" ] && in="/dev/stdin"
! [ -e ${in} ] && echo "\"${in}\" does not exist." > /dev/stderr && usage 2
! [ -r ${in} ] && echo "You do not have permission to read \"${in}\"." > /dev/stderr && usage 3
# Ouptut file must be writable
out="${2}"
if [ "${out}" == "" ]
then
  out="/dev/stdout"
else
  ! [ -w ${out} ] && echo "You do not have permission to write to \"${out}\"." > /dev/stderr && usage 4
  if [ -e "${out}" ]
  then
    read -t 5 -p "Do you want to overwrite \"${out}\"? (Y/n)" -N 1 ans
    [ $? -ne 0 ] && ans="Y"
    echo
    if [ "${ans^.}" == "Y" ]
    then
      rm -rf ${out}
    else
      echo "Output will be appended to \"${out}\"." > /dev/strerr
    fi
  fi
fi
# Main processing loop
i=0
while read
do
  i=$((${i}+1))
  if [ $((${i}%2)) -eq 1 ]
  then
    hold="${REPLY}"
  else
    echo ${hold} ${REPLY} >>${out}
  fi
done < ${in}
[ $((${i}%2)) -eq 1 ] && echo ${hold} >> ${out}


secondchanti 08-18-2010 09:56 AM

Dear PTrenholme,

While executing the shell, this following error is coming .
My inputfile is FILE!.

the error showing is

programme name : line 12 : $1 : ambiguous redirect.

Pl guide me

Thanks
Rao

grail 08-18-2010 10:33 AM

Please demonstrate how you are calling the script on the command line?

secondchanti 08-18-2010 10:47 AM

Friend grail,
Like below iam writing the awk programme

#!/bin/bash
i=0
while read
do
i=$((${i}+1))
if [ $((${i}%2)) -eq 0 ]
then
hold="${REPLY}"
else
echo ${hold} ${REPLY}
fi
done < $1
[ $((${i}%2)) -eq 0 ] && echo ${hold}
=====================
the error i showed as above is displaying.

Guide me
Rao

grail 08-18-2010 10:52 AM

Quote:

Like below iam writing the awk programme
ahhh ... no ... this does not contain the word 'awk' anywhere in it so it is definitely not an awk programme.

Also, please use [code][/code] tags when showing code as it is very hard to read all squashed up.

So this is the script you are executing. My question is, what are you typing on the command line to run it?

PTrenholme 08-18-2010 01:13 PM

Um, Rao, if you saved that code as a file, say "prog," then you could run it on two different ways:
  1. Make "prog" executable (chmod +x prog) and then do ./prog FILE.
  2. Run it explicitly: bash prog FILE

I note that you extracted the main loop from the code I provided, thus eliminating the sanity checks and help information. If you had run the code I provided, you should have received an error message and instructions if you failed to provide the correct arguments. (Any "good programming practices" course or book should tell you that you must always assume that users of your code will make mistakes when they do so, and that you should make your code as fool-proof as possible.)

P.S.: As grail noted, that is a shell script, not an AWK program. If you wish, I could provide an AWK program to do the same thing, but the shell script is, to me, easier to follow.

joec@home 08-18-2010 03:30 PM

I had a similar problem about a year back and i discovered that variable storage strips out carriage returns.

Code:

echo "
line 1
line 2
"

line 1
line 2

Code:

string=$(echo "
line 1
line 2
")

echo $string
line 1 line2


grail 08-18-2010 06:01 PM

Quote:

Originally Posted by joec
I had a similar problem about a year back and i discovered that variable storage strips out carriage returns.

Actually Joe, that information is not 100% correct. You will find that should you quote your variable with inverted commas that the whitespace will be maintained.
Code:

echo "$string"
What has happened is that wordsplitting occurs when a variable is unprotected from the shell. Therefore storing the lines in the variable is not the reason
for removing carriage returns.

joec@home 08-18-2010 08:37 PM

Now that is an interesting thing to note. So in this instance I actually wanted to remove the cariage returns, but adding the double quote to the variable allows you to return with the carriage returns.

Code:

string=$(echo "
line 1
line 2
")

echo $string
line 1 line 2

echo "$string"

line 1
line 2

While I do not need this information now, you never know what I might need in the future.


All times are GMT -5. The time now is 06:14 PM.