Reputation: 35
This is a linux problem So I need to make a folder called "Notes", and inside that folder I need to make 6 files. The files are named with ID number, as follows:
The first 3 letter are always "a00", and the 4th & 5th letter are the year. So I now need to copy the ID number/title of file without the '.txt' extension, and paste it on a new file outside the folder "Notes" Example file a00144998, it shows year 14(from the 4th & 5th letter), so I will copy a00144998 to a new file named "year14.txt" and sort it. Same as a00154667, it shows year 15, so I will copy a00154667 to a new file named "year15.txt". So at the end, file "year14.txt" will have :
a00143561
a00144998
I have found the code, and it works if the files are not in the folder. But once I create files inside folder, this code doesn't work, it keeps copying the txt extension. Any idea? Thanks!
ls ~/Notes/???14????.txt|sed 's/.\[4\]$//'|sort>year14.txt
Upvotes: 0
Views: 35
Reputation: 1058
No need for sed
, you can do it all in a bash oneliner:
for f in a0014*.txt; do echo ${f:5:4}; done | sort > year14.txt
This will loop over every file matching the glob a0014*.txt
and put each string in an f
variable, echo out 4
characters starting after the 5
th character.
TLDP has a great guide on string manipulation in bash.
You should also avoid parsing ls
. It's meant to be human and not machine readable.
Upvotes: 1