LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-23-2015, 06:32 AM   #1
shazo
LQ Newbie
 
Registered: Dec 2014
Posts: 12

Rep: Reputation: Disabled
how to select first elements of files.


hello,
i want to select first element from 2 to 3 files how i would do that. e.g my input files are
file1=(1,2,3,4)
file2 =(1,2,3,4)
file3=(1,2,3,4)
output should be
outputfile=(1,1,1)

Last edited by shazo; 01-23-2015 at 06:39 AM.
 
Old 01-23-2015, 08:27 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,962
Blog Entries: 12

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
For me I'd write a C program to do this. I'm assuming you can use a script, or something like sed and/or awk. You open the files, read them, validate what you're seeing, and then construct your result based on the brilliant algorithm you've designed.

What have you tried or considered? Have you considered the allowances and constraints on this whole problem?


... or is this an ASSIGNED problem?
 
Old 01-23-2015, 09:15 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Clearly rtmistler is seeing this clearly as for me, the question doesn't make much sense

Could you maybe explain further how you have a file that looks like:

file1=(1,2,3,4)

Does this mean the contents of file1 is a single line containing (1,2,3,4)?
 
Old 01-23-2015, 09:20 AM   #4
shazo
LQ Newbie
 
Registered: Dec 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
thanks for reply! all files contain many lines of numbers but i am intersted in first element of first line only.
i can use this code but with this i can not get my desire outpu for all files.
read word _ < file1
echo "$word"
 
Old 01-23-2015, 09:41 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,962
Blog Entries: 12

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
Quote:
Originally Posted by shazo View Post
thanks for reply! all files contain many lines of numbers but i am intersted in first element of first line only.
i can use this code but with this i can not get my desire outpu for all files.
read word _ < file1
echo "$word"
That a BASH script? Why don't you post all of what you have within CODE tags so that people can evaluate it. Because it's not yet clear how you are constructing your list of files to open and exactly what you mean by:
Quote:
i can not get my desire outpu for all files.
After all, using echo to output what you just read is "output", the question becomes one of whether or not that output matches the form you desired, and then next is how and where you redirect it.

Check my signature for assistance with placing code tags and also helpful links on BASH scripting. Also check my blog for some BASH script tips.
 
Old 01-23-2015, 09:57 AM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,165

Rep: Reputation: 889Reputation: 889Reputation: 889Reputation: 889Reputation: 889Reputation: 889Reputation: 889
it seems like awk would be the simplest method. you can have it filter on NR == 1 and have the FS = , ?
 
Old 01-26-2015, 05:35 AM   #7
shazo
LQ Newbie
 
Registered: Dec 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
can you please tell me how i can do it with awk?
 
Old 01-26-2015, 07:13 AM   #8
J Martin Rushton
Member
 
Registered: Jan 2015
Location: England
Distribution: Mainly CentOS
Posts: 31

Rep: Reputation: Disabled
I created three test files:
Code:
$ cat X.1
1,2,3
4,5,6
7,8,9
$ cat X.2
A,B,C
D,E,F
G,H,I
$ cat X.3
a,b,c
d,e,f
g,h,i
I then created the demo script:

Code:
#!/bin/sh
for fname in X.?
do
	IFS="," read wanted rubbish <$fname
	echo $wanted
done
and the output was:

Code:
1
A
a
Is that what you wanted? If not I'm sure you can hack it about as needed.

Last edited by J Martin Rushton; 01-26-2015 at 09:41 AM. Reason: correcting example
 
1 members found this post helpful.
Old 01-26-2015, 08:40 AM   #9
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,165

Rep: Reputation: 889Reputation: 889Reputation: 889Reputation: 889Reputation: 889Reputation: 889Reputation: 889
^ sounds like a homework question. we're not allowed to do peoples homework for them.
 
Old 01-26-2015, 08:50 AM   #10
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,962
Blog Entries: 12

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
Quote:
Originally Posted by J Martin Rushton View Post
I created three test files:
Code:
$ ls X.1
1,2,3
4,5,6
7,8,9
$ ls X.2
A,B,C
D,E,F
G,H,I
$ ls X.3
a,b,c
d,e,f
g,h,i
What in the world did you do in your profile? Alias cat to be ls?!? (Maybe it's a typo)

Please consider restraining the obvious answers for the posters who either put in zero effort for suspicious reasons.
 
Old 01-26-2015, 08:59 AM   #11
J Martin Rushton
Member
 
Registered: Jan 2015
Location: England
Distribution: Mainly CentOS
Posts: 31

Rep: Reputation: Disabled
Oops, yes. Cut-and-paste from on a Windoze box! It took so long to open that the first window was "sneaker mail" from the machine room, only the second a real paste!

Sorry.
 
Old 01-26-2015, 09:19 AM   #12
shazo
LQ Newbie
 
Registered: Dec 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
thank you! no it is not any homework. but i got idea thanks.
 
Old 01-26-2015, 09:32 AM   #13
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,962
Blog Entries: 12

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
Quote:
Originally Posted by shazo View Post
thank you! no it is not any homework. but i got idea thanks.
OK, no offense; however first two or so responses requested that you post "your attempts" because in line with LQ's manners of politeness, it's not nice to just ask and ask and ask without demonstrating that you're making your own attempts. The ideas are that you learn, but can learn with some helpful guidance.

The other parts noted that you could choose to write a program, write a script, or try a few commands from the command line and therefore some of that was discussed. You did mention awk, however you merely said "show me" or "tell me" versus saying "I tried <this> form of awk, and it didn't quite get me there. Recommendations?"

Further and finally, the problem isn't really scoped too well, especially since it's not a homework problem. Therefore you're doing it for fun, or for some other ulterior goal, and that goal may be far more complex, the content of all files to find and parse may be further complex, and so forth. Many times people give full information, such as "I'm just experimenting" or "I'm trying to develop a script to help me do blah-blah", and again the better way to get some good help is to post a sample of what you have. Just experimentation, then you can scope it as much as you wish.

If you "have the idea", meaning you've solved your question, why not consider posting your result or something representing it so as to offer help to a future reader of the thread who searches, wondering if their question has been asked before.
 
Old 01-26-2015, 09:49 AM   #14
shazo
LQ Newbie
 
Registered: Dec 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
i read first entity seperetly for each file
Quote:
read word _<file1
read word1 _<file2

read word2 _<file3

echo "{$word,$word1,$word2}"
i wanted to now simple comand to do this because i have many files. anyways thanks alot for all your help.
 
Old 01-26-2015, 11:07 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Once you have an answer or solution, please mark the question as SOLVED.
 
  


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 select all files except a group? fpp666 Programming 3 07-16-2009 08:31 AM
in copy files or ls files the command want to invert select some files how to?? hocheetiong Linux - Newbie 3 06-27-2008 07:32 AM
SuSE 9.3-You can only select local files bmk SUSE / openSUSE 8 09-08-2005 06:08 PM
GtkFileChooserDialog: How to select both, files and directories? froedi Programming 0 06-11-2005 08:07 AM
select files with attributes Quis Linux - General 1 04-03-2003 03:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:51 AM.

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