Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I had no idea how to word the title.

I need to take an output such as:

[~]# cat file
user1
domain1.com
user2
domain2.com
user3
domain3.com

and turn it into an output such as:

user1 domain1.com
user2 domain2.com
user3 domain3.com

Any assistance here would be greatly appreciated.

share|improve this question
3  
This will probably get moved over to Stack Overflow, and then closed as a duplicate of stackoverflow.com/questions/1513861/…. I had to strongly resist the urge to simply copy and paste the accepted answer over there to this thread, just for the potential rep points, but that would be cheating, IMHO. –  Jed Daniels Jun 21 '10 at 20:38
    
Yeah, I would have answered using sed, too. –  Dennis Williamson Jun 21 '10 at 20:45
add comment

3 Answers

up vote 9 down vote accepted
#!/bin/bash

while read user
do 
        read domain
        echo $user $domain     
done

usage:

cat file | scriptname
share|improve this answer
    
+1 -- Very nice! –  Christopher Karel Jun 21 '10 at 20:30
add comment

I figured this out with some array-age

for i in `seq 1 2 ${#array[@]}` ; do echo "${array[$i]} ${array[$i + 1]}"; done
share|improve this answer
add comment

Man, there's gotta be an easy way to do this. I clearly haven't got enough sleep, because I can't think of any. But, because I don't like arrays, I used a for loop. It essentially goes through each LINE, and outputs the even numbers to a new file without a linebreak, and appends the odd ones with a break. Seems to work!

COUNT=0
for LINE in $(cat file.txt);do
  if [ $(($COUNT % 2)) -eq 0 ];then 
      echo -n $LINE >> newfile.txt
      echo -n " " >> newfile.txt
  else
      echo $LINE >> newfile.txt
  fi
COUNT=$((COUNT + 1))
done



--Christopher Karel

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.