Get all last 4 characters of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get all last 4 characters of files
# 1  
Old 04-26-2016
Get all last 4 characters of files

Hi All.

Seeking for your assistance to get all the last 4 characters of each file before "." and put it in the variable

Code:
ex.
abcdZWU1501.csv
abcdXYZ1501.csv
abcdEFG1502.csv
abcdHIJ1501.csv

output will be:
Code:
1501
1502

What i did was
Code:
find *.csv | awk -F "." '{print $1}' | tail -c5
my output is 1502 only.

Please advise,

Thanks,
# 2  
Old 04-26-2016
If you use awk anyhow, why not do everything with it?
Code:
find *.csv | awk -F "." '{print substr ($1, length($1)-3)}'
1502
1501
1501
1501

Shell version (recent bash):
Code:
 while read FN; do T=${FN%.*}; echo ${T#${T%????}}; done < <(ls *.csv)
1502
1501
1501
1501

This User Gave Thanks to RudiC For This Post:
# 3  
Old 04-26-2016
Thanks Sir Rudic. what i did was i put it in the for loop statement.

BR,
# 4  
Old 04-26-2016
I believe the request was to take it one step further:
Code:
find . -name '*.csv'|awk '!((X=substr($0,length($0)-7,4)) in Y){print X;Y[X]}'

producing the output:
Code:
1502
1501

# 5  
Old 04-26-2016
I think this will help..

Code:
find *.csv | cut -d "." -f1 | rev | cut -c1-4 | rev

it will produce output like below

Code:
1501
1501
1502
1501

# 6  
Old 04-26-2016
A tad bit compact Smilie
Code:
find *.csv | awk -F"[^0-9]+" '{print $2}'

# 7  
Old 04-26-2016
try also:
Code:
find *.csv | sed 's/.*\(....\)[.]csv/\1/'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove duplicates from two files using n characters for comparison

Hi All, I have two files file1 123456CRTGHG 125437CRNDGF 126537CRDDGF file2 123456CRTZHC 124567CJHGHG 125987CJHGDF I need to compare the two files and any records in file 1 and 2 based on initial n characters (6 in example) need to be ignored. string separated by unprintable... (2 Replies)
Discussion started by: Bruble
2 Replies

2. UNIX for Dummies Questions & Answers

remove characters from list of files

done some homework on this-- after i remove up to and including the ) i want to take newfile.txt and use that list to remove the files from a file in my the directory pwd i have a input.txt file cat input,txt 1)mary.jpg 12)john.jpg 100)frankkfkdf .jpg i want to remove the characters in the... (1 Reply)
Discussion started by: plener
1 Replies

3. UNIX for Dummies Questions & Answers

How to remove characters from multiple .txt files

Friends, I want to remove charecters from multiple .txt files. Foe example : In this .txt files there are many "ctrl m" present in last of each line in one .txt file. I want to remove "ctrl m" from each line from all .txt files. Need your help regarding this. (4 Replies)
Discussion started by: meetsubhas
4 Replies

4. UNIX for Dummies Questions & Answers

Files with special characters - how to remove

Hi, I have a directory that has a file which contained special characters in the filename. Can someone please advise how to remove the file, preferably with a rm -i ? Thanks in advance. Listing is as below: {oracle}> ls -1b bplog.bkup.001 bplog.bkup.002 bplog.bkup.003 bplog.bkup.004... (1 Reply)
Discussion started by: newbie_01
1 Replies

5. UNIX for Dummies Questions & Answers

Find in Files (special characters)

Well, I've searched the forum, but couldn't find an option, that would help me. I'm really a dummie in unix, so here it goes. I've got like 50k files in a single catalogue. One of them contains a string: Including the box/square brackets. I tried to find it manually, and use some search... (2 Replies)
Discussion started by: kalik
2 Replies

6. Shell Programming and Scripting

Replace Characters for bunch of Files.

Hi, I am new to unix and looking out for some help in reading a file contents and replacing the characters, the requirement is I having a folder and having nearly 300 txt files, all the file contents contains some words we need to iterate all each and every files and need to find and replace it... (1 Reply)
Discussion started by: subrahmaniank
1 Replies

7. Shell Programming and Scripting

copying files with dumb characters

hello I'm trying to batch copy files from one location to another. My script get's the output of a find command (i.e. find /disk3/jpm/seq -type f | xargs copy2boss) The script works fine, except when filenames contains whitespace, backslashes and so on. Any hints? Is there another more accurate... (2 Replies)
Discussion started by: kaklon
2 Replies

8. Shell Programming and Scripting

how do I identify files with characters beyond a certain range.

I have a directory with hundreds of files that can not have data pass column 80. I do not know of way to combine "grep" and "cut" command. I tried: cat * | cut -c 81-120 |pg but it only shows me the line, not the file name. Any help would be appreciated. Been on this all... (3 Replies)
Discussion started by: kcsunsun01dev
3 Replies

9. Shell Programming and Scripting

Search For Control M characters in files

Hi , I have special character control M in many of my files as below ersNet-Telnet-3.03/Makefile.PL100644 21166 144 612 7113770214 135 77 0ustar jayusers## -*- Perl -*-^M ^M use ExtUtils::MakeMaker qw(WriteMakefile);^M ^M WriteMakefile(NAME => "Net::Telnet",^M ... (4 Replies)
Discussion started by: Mohammed
4 Replies

10. Shell Programming and Scripting

Best way to search files for non-printable characters?

I need to check ftp'd incoming files for characters that are not alphanumeric,<tab>, <cr>, or <lf> characters. Each file would have 10-20,000 line with up to 3,000 characters per line. Should I use awk, sed, or grep and what would the command look like to do such a search? Thanks much to anyone... (2 Replies)
Discussion started by: jvander
2 Replies
Login or Register to Ask a Question