LinuxQuestions.org
Help answer threads with 0 replies.
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 02-23-2008, 09:11 PM   #1
chakjoy
LQ Newbie
 
Registered: Feb 2008
Posts: 5
Blog Entries: 2

Rep: Reputation: 0
some help with read and write


Hi,

I have two files, 'file1' and 'file2'. 'file1' looks like:

12345678
87654321
24681357
13572468

and 'file2' looks like the following:

abcdefgh
ijklmnop
qrstuvwx
yzabcdef

I am trying to read from these files line by line and create a file that should look like the following:

12345678 | abcdefgh
87654321 | ijklmnop
24681357 | qrstuvwx
13572468 | yzabcdef

Please let me know how I can do that.

Thanks for your help.

Joy
 
Old 02-23-2008, 09:46 PM   #2
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 370

Rep: Reputation: 30
The problem here is that if the files are not of same length, you will have to decide to break the look somewhere...

If on the other hand, they are of same length, this might help:
Code:
% ./a.sh
12345678 | abcdefgh
87654321 | ijklmnop
24681357 | qrstuvwx
13572468 | yzabcdef
% cat a.sh
#!/bin/zsh
exec 9<file2
cat file1|while read line1; do
	read -u 9 line2
	echo "$line1 | $line2"
done
exec 9<&-
 
Old 02-23-2008, 10:02 PM   #3
chakjoy
LQ Newbie
 
Registered: Feb 2008
Posts: 5
Blog Entries: 2

Original Poster
Rep: Reputation: 0
Hi,

Thanks a lot. Yes it works.
Just wondering if there are other options as well.

Joy
 
Old 02-23-2008, 10:06 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
paste -d"|" file1 file2



Cheers,
Tink
 
Old 02-23-2008, 11:10 PM   #5
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 370

Rep: Reputation: 30
Hahaha! I didn't know of it

You learn something new everyday.
 
Old 02-23-2008, 11:12 PM   #6
chakjoy
LQ Newbie
 
Registered: Feb 2008
Posts: 5
Blog Entries: 2

Original Poster
Rep: Reputation: 0
Thanks Guys,

Both work sweet.

You guys are awesome.

Cheers
Joy
 
Old 03-17-2008, 01:27 PM   #7
chakjoy
LQ Newbie
 
Registered: Feb 2008
Posts: 5
Blog Entries: 2

Original Poster
Rep: Reputation: 0
I have a file test.txt like the following:

Name | ID | Details | Comment
data1 data2 data3 data4
data5 data6 data7 data8
data9 data10 data11 data12

I want to read from this file from the second row (where the actual data lies) as follows:

while read line
do
echo $line >> $out_file
done < test.txt

How can I do that
 
Old 03-17-2008, 02:26 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
awk '{print $2}' infile > outfile


Cheers,
Tink
 
  


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
How can I have: Root has Read-Write, user has read only for the boot filesystem? xmrkite Linux - Software 6 10-16-2008 04:31 AM
Read/write locks using semaphores with write priority arunka Programming 1 06-28-2007 07:04 PM
Mounted read-write Fat32 partition suddenly becomes read-only?? hohead Linux - General 8 04-05-2007 12:17 PM
Why can I not write to a SAMBA share when read/write is enabled? eric m Linux - General 4 08-21-2006 09:22 PM
Open office read only, K-write read/write mode lwtvh Linux - Newbie 1 07-19-2003 11:33 AM


All times are GMT -5. The time now is 06:55 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