Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm afraid what you've given us is nothing but a long list of numbers, letters and dates. How are you going from one set of data to the other? None of my guesses seem to be consistent with the data you've given.
In any case, it looks like it's going to be a more complicated parsing job than just a simple sed/awk script (though I'm sure it could be done that way) so you'd be better off with python or perl or something like that.
please note the pattern in the first column the conetent is the same, i just want to have all the fields of that column fow which its the same in the same row
thaks though wen i look at the code it seems to suggest the first column changeds in 3's whereas at some points it changes after four of 5, ie 5 a's it could be
It can be achieved simply by a piece of AWK code. I believe it would be much better than the tedious and heavy python codes.
And, it is more general than the previous solution from Snark1994, since here the code can generate the first line according to the file itself rather than specifying it manually as "n 1 2 3 D1 D2 D3".
It dose not matter if you change your file to include more than 3 lines for a, b or c. The code bellow are able to adjust it according to the input file.
Code:
awk '!/^$/{
f1=$2
a[$2]=a[$2]$4" "
b[$2]=b[$2]$5" "
c[$2]=c[$2]$3" "
split(c[f1],d," ")
}
END {
printf "1. n "c[f1]
for (i in d) printf "D"d[i]" "
printf "\n"
for (i in a) {
k++
printf k+1". "i " "a[i]b[i]
printf "\n"}
}
' YOURINPUTFILE
It always makes me cry on the inside a little when you do that, grail.
Sorry, arn2025, I didn't quite understand where you got your header line from - my code would work fine (it would print 4 or 5 numbers on the line) but it would only put "1 2 3" in the header. It would be easy to change it to do this properly too, but seeing as you've got two other solutions, I'll leave you with those.
(Also, if you look, neither grail nor I found it easy to work out what exactly you needed doing - even when the gist of it was clear, the fact that you might get 4 or 5 similar rows, or the presence of a header, was not obvious. Next time you post a thread, perhaps give a bit of thought to explaining what you want clearly and precisely at the start. Just a heads up, I hope I'm not lecturing )
If you consider this problem to be solved, can you mark the thread as 'SOLVED' please? Thank you.
Goodness no, I meant post a completely cryptic one-liner which baffles me, after I write a 20-line script to do it :L
Well I did start to teach myself python3 a while back, and do still quite enjoy its benefits, but since having been shown a little bit of ruby from another
LQ member I got stuck in and really enjoy it
You are right though, I need to remember to explain them a bit better
Code:
-a - Split read lines using the default delimiter into the global array $F
-n - Read in a file
-e - The following is a script to be interpreted
BEGIN{l=[]} - initialize the 'l' array (BEGIN here is the same as awk, ie only read once
if ! l.empty? && l[0] != $F[0] # l array not empty and first element in l and $F arrays not equal
puts l.join(" ") # Display the contents of l array separated by a space
l.clear # Reset l array
else
l<<$F[0] if l.empty? # like perl simple tests may come after the action. << means append to array
l<<$F.last
l.insert((l.count/2).ceil,$F[2]) # insert an item at given position. ceil is to round up to the nearest whole number
end
Mm, I do like ruby, mostly because of its support of functional programming - it bridged the gap between haskell and python, because I got to do a lot of the neat haskell tricks without having the hassle of very strict type checking and pure functions.
Yeah, definitely very nifty code
@cristalp, have we solved your problem? If so, please remember to mark the thread as solved.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.