Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
Help! The group I work with just switched from a Unix based computer to a Linux based computer. everything works pretty much the same way but sort. On Unix I could do a merge and sort on a huge SLR monthly file and it would put it in cronical order with out much troubles. In Linux it runs into the blanks and give me day 1-10 then day 2-20 ect.
I know I can do all the blanks to zero but I don't want to do that because it will change the standardized format the data is in, but what I need to know is, is there a way to replace the any blanks that occur in the first 27 columes of every line with a zero in sed.?
any advice on sed or sort would be greatly appicated
Ruth
what exactly do the strings you want to sort look like? you can pass sort the -t option to tell it what to use as a field delimieter, then use -k to specify what field to sort by first, second and so on and -n to use numberic sort.
Have you checked the man page for sort?
Also, I suggest the Bash Guide for Beginners by Machtelt Garrels--it's on TLDP
I have checked out the man pages and I also have Linux in a Nutshell
plus I have bugged any one I can find who know any thing about Linux. What's TLDp Mean?
sirclif-
Here is an example of the line in the files I am trying to sort.
(This is all one line wraped)
These are monthly Satellite Laser Ranging(SLR) files. The first seven
columns is the satalite number. Column 8-27 is the date. that the only area I need to replace any blank that occur with zero. Then I can sort it using a general numeric sort and it will sort the right way.
ok, im sorry, but im having a hard time figureing out what you mean by 'column'. is each number a columen, or each group of numbers seperated by a space?
does this number represent the date --> 03 16778215830105782?
if so, how? i'm sure we can find a regular expression to do what you need, but i'm having trouble understanding the data format.
I'm with Sirclif, no idea what the criteria are you're trying
to sort by, nor can I see anything that I'd pick to be the re-
presentation of a date in your sample.
If the problem is caused by the space (or the alignment of numerical
values) you can use -g to tell it to sort those by value rather than
by ASCII codes.
sirclif
that is the date it's the year 03 the julian day 167 and the time in
Hunderds of seconds ending at 10, the 5782 is not part of the time its the beginning of the ground station number where the data is collected. so it clairfy the date is (03 1677821583010) This is the only part of the line which I need the blank replaced by zero's in. It is the part I sort by.
Tinkster
I tryed using -g it runs into the blanks and sorts
1
11
12
13
14
15
16
17
18
19
2
20
ect.
If I use the -u to get rid of any duplicate lines it runs into the blanks
thinks all other lines are the same and gives me only one line.
ok i think i understand your problem now. i think this will work.
Code:
$ cat file.SLR | sed "s/^.\{7\}\(.\{16\}\).*/\1 X\0/" | sort -gk1,2 | sed "s/^.*X//"
this will...
display contents of file, send it to sed
sed then skips the first 7 characters ( the date starts at character 8), saves the next 17 characters (03 1677821583010 is 16 characters), then matches to the rest of the line.
sed replaces the line with a line that has the 16 important characters at the front, then a space, then an 'X', then the original line, so...
this is passed to sort, which does a general numeric sort (-g), first sorts by column 1 ("03" specified by -k1) then column 2 ("1677821583010" by ,2). This may be overkill, simply using sort worked for me, but this will make sure for you.
the sorted list is passed back to sed which removes the string at the beginning of the line we put there to sort by matching all characters up to an 'X' and replacing it with nothing. this should work even if an 'X' is present in the line, because sed will only match to the first X it finds.
hope this is what your looking for.
also, Tinkster may have some suggestions on making the sed command more readable, it is cluttered with '\' to excape the metacharacters.
I'm still curious: which Unix were you using, and what was
the original invocation of the sort on that platform?
And can you please post an example of more than one row, and
what the sort should give you as well as what you get?
I've tried (based on the description of what you and Sirclif
were talking about ) with a little dummy file (copied your one
line repeatedly, and edited a few of the julian days), and a
plain sort without any further parameters seems to do what I'd
have expected.
Cheers,
Tink
P.S.: Sirclif: unless you stick your sed-command in a file
there's no way I'm aware off of escaping the escapes ;}
uncompress 7*_$kt*.$lt.Z
set error = $status
if ($error == 1) then
goto next1
endif
sort -u -m -o $lt_a.$kt *$kt*.$lt
compress $lt_a.$kt
"slrmerg6" 30L, 455C 3,1 Top
If I just wanted to resort a file after a corrected bad data I would use
sort -u -o glonass89_b1.0305 glonass89_b.0305
One of my smaller files is 75000 line long
Here is an example of the results of the program above on linux
sirclif
the problem with your suggestion is moving the date and satellite number corrupts the format of the data . Also depending on the month and the seconds
such 12:01 AM the can be mor than one blank in the time
the format is only "corrupted" when it is sent to sort, then "uncorrupted" by passing through sed again. so what is printed to the screen looks just like what you have shown. you can also modify the regular expression to be more general to account for such cases.
ok, i'm seeing your problem now. so how do you know how many digits the day takes up? in the above example, how can you tell that it's day 10 and not day 101? does the lenght of the string in the second column change? the year is the last two digits of the first columns string right? but the day could be either the first, first two, or first three digits of the second columns string?
sorry if im being a little slow here, but i dont fully understand the format you are requireing.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.