LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-08-2010, 01:09 PM   #1
secondchanti
LQ Newbie
 
Registered: Jul 2010
Posts: 23

Rep: Reputation: 15
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
 
Old 08-08-2010, 01:18 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,933

Rep: Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692
Quote:
Originally Posted by secondchanti View Post
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.
 
Old 08-08-2010, 01:36 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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>

Last edited by PTrenholme; 08-08-2010 at 02:33 PM.
 
Old 08-08-2010, 10:17 PM   #4
secondchanti
LQ Newbie
 
Registered: Jul 2010
Posts: 23

Original Poster
Rep: Reputation: 15
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
 
Old 08-09-2010, 06:44 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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! 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}
 
Old 08-18-2010, 10:56 AM   #6
secondchanti
LQ Newbie
 
Registered: Jul 2010
Posts: 23

Original Poster
Rep: Reputation: 15
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
 
Old 08-18-2010, 11:33 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Please demonstrate how you are calling the script on the command line?
 
Old 08-18-2010, 11:47 AM   #8
secondchanti
LQ Newbie
 
Registered: Jul 2010
Posts: 23

Original Poster
Rep: Reputation: 15
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
 
Old 08-18-2010, 11:52 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
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?
 
Old 08-18-2010, 02:13 PM   #10
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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.
 
Old 08-18-2010, 04:30 PM   #11
joec@home
Member
 
Registered: Sep 2009
Location: Galveston Tx
Posts: 290

Rep: Reputation: 70
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

Last edited by joec@home; 08-18-2010 at 09:33 PM.
 
Old 08-18-2010, 07:01 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
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.
 
Old 08-18-2010, 09:37 PM   #13
joec@home
Member
 
Registered: Sep 2009
Location: Galveston Tx
Posts: 290

Rep: Reputation: 70
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Comparing Kernel Dmesgs: Remove Timing Info and Diff Side by Side LXer Syndicated Linux News 0 08-04-2010 08:31 PM
how can I view two windows side by side on my desktop Ubuntu 9.10 izquierdista Ubuntu 8 11-17-2009 01:52 PM
X11 and TEXT mode dual monitor side by side marius_c Linux - General 0 10-30-2009 11:30 AM
Installing SUSE 11.1 side by side with Ubuntu already installed on a USB drive jjchavez Linux - Laptop and Netbook 2 09-12-2009 02:39 PM
LXer: Top Linux photo managers side-by-side LXer Syndicated Linux News 0 12-14-2006 07:33 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration