[SOLVED] Sort lines by date

Need help with C, C++, perl, python, etc?

[SOLVED] Sort lines by date

Postby gaijin » 2016-11-29 13:48

Hello I have file.txt. I want to change this:
Code: Select all
Z nad n contrahendo / Anna Zachiewicz // W: Rota : ofiarowane Panu wi. - Klucork : Kluck Sp. z o.o., [2007]. - s. 331-354
Zachowe rezsji nad pkowego / Mhariasiewicz // Rejent. - 1996, nr 2, s. 180-202
Zasada mownych / Marsiewicz // Wwa prywatnego : kwa dedykowana ProfOleszce. - Waszy RP, (2012). - s. 550-566
Zasada dny stron w : ("culptrahendo") / Mhariasiewicz // W: Rwnicze : profesora Ma. - Kasa : "Zakaze", 1944. - s. 1501-1518


to this:
Code: Select all
Zasada mownych / Marsiewicz // Wwa prywatnego : kwa dedykowana ProfOleszce. - Waszy RP, (2012). - s. 550-566
Z nad n contrahendo / Anna Zachiewicz // W: Rota : ofiarowane Panu wi. - Klucork : Kluck Sp. z o.o., [2007]. - s. 331-354
Zachowe rezsji nad pkowego / Mhariasiewicz // Rejent. - 1996, nr 2, s. 180-202
Zasada dny stron w : ("culptrahendo") / Mhariasiewicz // W: Rwnicze : profesora Ma. - Kasa : "Zakaze", 1944. - s. 1501-1518


I want sort this lines descending by date:
Code: Select all
grep "[1-2][0,9][0-9][0-9]" file.txt


Date is bettween 1900 - 2999, but the date is on each line on diffrent position and place: [2007]; 1996; (2012)
Help.
Last edited by gaijin on 2016-12-01 11:44, edited 1 time in total.
gaijin
 
Posts: 7
Joined: 2016-11-22 12:08

Re: Sort lines by date

Postby kiyop » 2016-11-29 14:49

Another homework?
Please pay me ;)
Code: Select all
#!/bin/bash
sed -e 's/.*\(19[0-9][0-9]\|2[0-9][0-9][0-9]\).*/\1/' file.txt|nl -n ln -s " "|tr -s " "|sed -e "s/\(^[0-9]*\) \(.*\)/\2 \1/"|sort|sed -e "s/.* \([0-9]*\)$/\1/"|grep '^[0-9]\+$' > /tmp/test
cat /tmp/test|while read num
do sed -n ${num}p file.txt
done
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/
User avatar
kiyop
 
Posts: 3984
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan

Re: Sort lines by date

Postby dasein » 2016-11-29 15:18

DYODH

Seriously.
User avatar
dasein
 
Posts: 7768
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: Sort lines by date

Postby GarryRicketson » 2016-11-29 15:35

What does
DYODH
mean ?
I had to look that one up,........





Forum guidelines. Please read before first post!
User avatar
GarryRicketson
 
Posts: 4117
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Sort lines by date

Postby gaijin » 2016-11-30 07:50

kiyop wrote:Another homework?
Please pay me ;)
Code: Select all
#!/bin/bash
sed -e 's/.*\(19[0-9][0-9]\|2[0-9][0-9][0-9]\).*/\1/' file.txt|nl -n ln -s " "|tr -s " "|sed -e "s/\(^[0-9]*\) \(.*\)/\2 \1/"|sort|sed -e "s/.* \([0-9]*\)$/\1/"|grep '^[0-9]\+$' > /tmp/test
cat /tmp/test|while read num
do sed -n ${num}p file.txt
done


THANK YOU
OK. But it only save status this lines to /tmp/test and show on console good version
How to save good version to /tmp/test?

start file:
Code: Select all
cat file.txt
Z nad n contrahendo / Anna Zachiewicz // W: Rota : ofiarowane Panu wi. - Klucork : Kluck Sp. z o.o., [2007]. - s. 331-354
Zachowe rezsji nad pkowego / Mhariasiewicz // Rejent. - 1996, nr 2, s. 180-202
Zasada mownych / Marsiewicz // Wwa prywatnego : kwa dedykowana ProfOleszce. - Waszy RP, (2012). - s. 550-566
Zasada dny stron w : ("culptrahendo") / Mhariasiewicz // W: Rwnicze : profesora Ma. - Kasa : "Zakaze", 1944. - s. 1501-1518


console:
Code: Select all
./4.sh
Zasada mownych / Marsiewicz // Wwa prywatnego : kwa dedykowana ProfOleszce. - Waszy RP, (2012). - s. 550-566
Z nad n contrahendo / Anna Zachiewicz // W: Rota : ofiarowane Panu wi. - Klucork : Kluck Sp. z o.o., [2007]. - s. 331-354
Zachowe rezsji nad pkowego / Mhariasiewicz // Rejent. - 1996, nr 2, s. 180-202
Zasada dny stron w : ("culptrahendo") / Mhariasiewicz // W: Rwnicze : profesora Ma. - Kasa : "Zakaze", 1944. - s. 1501-1518


saved file
Code: Select all
cat test
3
1
2
4



errata
work ok:
./4.sh > newfile.txt

...but how it make for many files?

Code: Select all
#!/bin/bash
cd manyfiles
for file in *.txt ; do
sed -e 's/.*\(19[4-9][0-9]\|20[0-1][0-9]\).*/\1/' $file|nl -n ln -s " "|tr -s " "|sed -e "s/\(^[0-9]*\) \(.*\)/\2 \1/"|sort -r|sed -e "s/.* \([0-9]*\)$/\1/"|grep '^[0-9]\+$' > a/$file
cat a/$file|while read num
do sed -n ${num}p $file;
done; 
done


./4.sh > all.txt
make all files to one file...


Code: Select all
do sed -n ${num}p $file > new/$file;


it generates files from one line of the oldest date


I found it
Code: Select all
...
do sed -n ${num}p $file;
done > ok/$file;  done...


THANK YOU for your time and knowledge...
Last edited by gaijin on 2016-12-05 07:30, edited 1 time in total.
gaijin
 
Posts: 7
Joined: 2016-11-22 12:08

Re: Sort lines by date

Postby kiyop » 2016-12-01 14:15

Congratulations on your success :)
But please correct the above post of yours.
What you quoted in the post is not same as what I posted.

viewtopic.php?f=8&t=130651&p=630251#p630186
kiyop wrote:
Code: Select all
sed -e 's/.*\(19[0-9][0-9]\|2[0-9][0-9][0-9]\).*/\1/' file.txt|nl -n ln -s " "|tr -s " "|sed -e "s/\(^[0-9]*\) \(.*\)/\2 \1/"|sort|sed -e "s/.* \([0-9]*\)$/\1/"|grep '^[0-9]\+$' > /tmp/test


viewtopic.php?f=8&t=130651&p=630251#p630251
gaijin wrote:
kiyop wrote:
Code: Select all
sed -e 's/.*\(19[0-9][0-9]\|2[0-9][0-9][0-9]\).*/\1/' file.txt|nl -n ln -s " "|tr -s " "|sed -e "s/\(^[0-9]*\) \(.*\)/\2 \1/"|sort -r|sed -e "s/.* \([0-9]*\)$/\1/"|grep '^[0-9]\+$' > /tmp/test


I posted "sort" without "-r".

I misunderstood what you wanted and I posted a bad script.


gaijin wrote:
Code: Select all
do sed -n ${num}p $file > new/$file;


it generates files from one line of the oldest date

I found it
Code: Select all
...
do sed -n ${num}p $file;
done > ok/$file;  done...

">>" is also useful.

Refer, for example, https://www.tutorialspoint.com/unix/uni ... ctions.htm

There are many possible problems.
test.sh
Code: Select all
#!/bin/bash
if [ -e ok/$1 ];then
 echo "Warning! There is $PWD/ok/$1 . I will output results to $PWD/ok/$1 . May I detele $PWD/ok/$1 ? Yes=ENTER NO=other If \"No\", I will add results to the end of $PWD/ok/$1"
 read a
 if [ -z "$a" ];then
  rm -rf ok/$1
 fi
fi
sed -e 's/.*\(19[0-9][0-9]\|2[0-9][0-9][0-9]\).*/\1/' $1|nl -n ln -s " "|tr -s " "|sed -e "s/\(^[0-9]*\) \(.*\)/\2 \1/"|sort -r|sed -e "s/.* \([0-9]*\)$/\1/"|grep '^[0-9]\+$' > /tmp/test
cat /tmp/test|while read num;do
 sed -n ${num}p $1 >> ok/$1
done
rm -rf /tmp/test


file.txt
Code: Select all
123 12346567890 1994
1994 2015 1985
1985 2014 1994
gjl johard g 2000 jl glas
gla lgh aflgj oiahr hlsa 1978  gjaldgj l
hgjlh jlgdj lhr
ghlahj alhj l
hjla hjh
hglajh l hla 3000  ghlhjal h
ghalgjal hjl  2050 lojhdal


Code: Select all
mkdir ok
bash ./test.sh file.txt
more ok/file.txt

gives:
hjla hjh
hglajh l hla 3000 ghlhjal h
hgjlh jlgdj lhr
ghlahj alhj l
ghalgjal hjl 2050 lojhdal
gjl johard g 2000 jl glas
1985 2014 1994
123 12346567890 1994
1994 2015 1985
gla lgh aflgj oiahr hlsa 1978 gjaldgj l


Enjoy making scripts :)
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/
User avatar
kiyop
 
Posts: 3984
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan

Re: Sort lines by date

Postby gaijin » 2016-12-02 10:14

kiyop wrote:There are many possible problems.
Enjoy making scripts :)

Yes I know.
"My" script is now:
Code: Select all
#!/bin/bash
cd szukaj
for file in *.txt ; do
sed -e 's/.*\(19[4-9][0-9]\|20[0-1][0-7]\).*/\1/' $file|nl -n ln -s " "|tr -s " "|sed -e "s/\(^[0-9]*\) \(.*\)/\2 \1/"|sort -r|sed -e "s/.* \([0-9]*\)$/\1/"|grep '^[0-9]\+$' > tmp/$file
cat tmp/$file|while read num
do sed -n ${num}p $file;
done >gotowe/$file;  done


-r because I want from the biggest date to the smallest
On every lines is date and I know sometimes is two dates and number of page. I must accept this fact. Some solution is specify searching from 1949 to 2017.

In Poland we have a proverb: it's not my tale
programming it's not my tale :)
but learning and thinking and making script is interesting

Thank You very much
gaijin
 
Posts: 7
Joined: 2016-11-22 12:08

Re: [SOLVED] Sort lines by date

Postby kiyop » 2016-12-03 06:22

Please modify your post: viewtopic.php?f=8&t=130651&p=630392#p630251
Please replace the wrong line:
gaijin wrote:
kiyop wrote:
Code: Select all
sed -e 's/.*\(19[0-9][0-9]\|2[0-9][0-9][0-9]\).*/\1/' file.txt|nl -n ln -s " "|tr -s " "|sed -e "s/\(^[0-9]*\) \(.*\)/\2 \1/"|sort -r|sed -e "s/.* \([0-9]*\)$/\1/"|grep '^[0-9]\+$' > /tmp/test

with the correct one
kiyop wrote:
Code: Select all
sed -e 's/.*\(19[0-9][0-9]\|2[0-9][0-9][0-9]\).*/\1/' file.txt|nl -n ln -s " "|tr -s " "|sed -e "s/\(^[0-9]*\) \(.*\)/\2 \1/"|sort|sed -e "s/.* \([0-9]*\)$/\1/"|grep '^[0-9]\+$' > /tmp/test


In other words, please remove "-r" from
gaijin wrote:
kiyop wrote:
Code: Select all
sed -e 's/.*\(19[0-9][0-9]\|2[0-9][0-9][0-9]\).*/\1/' file.txt|nl -n ln -s " "|tr -s " "|sed -e "s/\(^[0-9]*\) \(.*\)/\2 \1/"|sort -r|sed -e "s/.* \([0-9]*\)$/\1/"|grep '^[0-9]\+$' > /tmp/test


DO NOT describe that I posted a different thing from what I posted.
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/
User avatar
kiyop
 
Posts: 3984
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan

Re: [SOLVED] Sort lines by date

Postby gaijin » 2016-12-05 07:32

kiyop wrote:DO NOT describe that I posted a different thing from what I posted.

Sorry - done.
gaijin
 
Posts: 7
Joined: 2016-11-22 12:08

Re: [SOLVED] Sort lines by date

Postby kiyop » 2016-12-05 12:59

gaijin wrote:
kiyop wrote:DO NOT describe that I posted a different thing from what I posted.

Sorry - done.

Thank you for correcting :)
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/
User avatar
kiyop
 
Posts: 3984
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests

fashionable