Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[SOLVED] Sort lines by date

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
gaijin
Posts: 16
Joined: 2016-11-22 12:08

[SOLVED] Sort lines by date

#1 Post by gaijin »

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.

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: Sort lines by date

#2 Post by kiyop »

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
dasein
Posts: 7680
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: Sort lines by date

#3 Post by dasein »

DYODH

Seriously.

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Sort lines by date

#4 Post by GarryRicketson »

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





Forum guidelines. Please read before first post!

gaijin
Posts: 16
Joined: 2016-11-22 12:08

Re: Sort lines by date

#5 Post by gaijin »

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.

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: Sort lines by date

#6 Post by kiyop »

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.

http://forums.debian.net/viewtopic.php? ... 51#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
http://forums.debian.net/viewtopic.php? ... 51#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/

gaijin
Posts: 16
Joined: 2016-11-22 12:08

Re: Sort lines by date

#7 Post by gaijin »

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

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: [SOLVED] Sort lines by date

#8 Post by kiyop »

Please modify your post: http://forums.debian.net/viewtopic.php? ... 92#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/

gaijin
Posts: 16
Joined: 2016-11-22 12:08

Re: [SOLVED] Sort lines by date

#9 Post by gaijin »

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

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: [SOLVED] Sort lines by date

#10 Post by kiyop »

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/

Post Reply