Find first 5 characters in the file name


 
Thread Tools Search this Thread
Top Forums Programming Find first 5 characters in the file name
# 1  
Old 03-27-2014
Find first 5 characters in the file name

Hi,
I have some files in a folder.
I need to find first 5 characters in the file name of every file and write those into a file.

Example:
ABC126_ACCPT.txt
AYA127_ACCPT.txt

Output in the file:
ABC126
AYA127
# 2  
Old 03-27-2014
Code:
ls *\_*.txt | while read file;do echo $file | cut -d_ -f1 >> output.txt;done

cat output.txt
ABC126
AYA127

# 3  
Old 03-27-2014
Firstly there are 6 characters as shown, not 5 as described...
Secondly what have you tried?
Thirdly simple pseudo_code...
Code:
create a new file of say /tmp/filenames.lst
change the drawer to place where *.txt files are situated
ls *.txt > /tmp/listing
[optional]cat < /tmp/listing [as a visible check.]
read in the /tmp/listing to a var
create an array from var
use a loop derived from the number of array elements<and>
from each line of the array extract the first 6 characters
echo <each_set_of_6_characters> and append to /tmp/filenames.lst

# 4  
Old 03-27-2014
If available:
Code:
ls|colrm 6

These 2 Users Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find new line characters in a file

Hi All, I need to find new line characters in a file and their location in the file and print the results. can someone pleas help. KM (4 Replies)
Discussion started by: Kevin Tivoli
4 Replies

2. Shell Programming and Scripting

Find out special characters from xml file

Hi....I have a xml file which is having lots of special characters which I need to find out and put the distinct list of those into a text file. The list of special characters is not specific, it can be anything at different point of time. Can anyone help me to find out the same and list out? ... (10 Replies)
Discussion started by: Krishanu Saha
10 Replies

3. Shell Programming and Scripting

HOw to find special characters

I have flat file which has data like this glid¿as_liste¿025175456 How can I print these lines into new file? (4 Replies)
Discussion started by: sol_nov
4 Replies

4. Shell Programming and Scripting

Find word in file then get following characters

Hello, I have several xml files from which I want to find and return a particular string I want to locate the InId="00000008". Now that is inlcuded within a tag and ofcourse the number is different every time this is what I came up with given that after greping the line that contains the... (6 Replies)
Discussion started by: TasosARISFC
6 Replies

5. UNIX for Dummies Questions & Answers

find a word in a file, plus the next 6 characters?

I plan to use sed in a script to replace a string. My problem is the last 6 characters of the word to be replaced can be different each time, plus it's not always in the same spot on the line so I can't use cut or nawk to get the field. So I am looking for a way to find a certain word in a file,... (6 Replies)
Discussion started by: mikayla73
6 Replies

6. UNIX for Dummies Questions & Answers

shell script to find noof characters in a file name

hiiii shell script to find noof characters in a file name, when you run ls -l (using awk) I tried with this ls -l > temp awk -F"," '{print $1 " " expr length $9}' temp but it give some other value instead of file name length (error value like , 563,54,55,56....).How to prnint the... (10 Replies)
Discussion started by: krishnampkkm
10 Replies

7. UNIX for Dummies Questions & Answers

Using grep to find one or more characters

Hi I am a little stuck using grep. I want to be able to find from a file all lines which have the sequence of characters t, followed by any character, followed by the characters ing. I have tried looking at the man pages for help, but did not understand it correctly (as it is not the most... (9 Replies)
Discussion started by: rushhour
9 Replies

8. UNIX for Dummies Questions & Answers

Find and replace special characters in a file

HI All I need a shell script ehich removes all special characters from file and converts the file to UTF-* format Specail characters to be removed must be configurable. strIllegal = @"?/><,:;""'{|\\+=-)(*&^%$#@!~`"; Please help me in getting this script as my scripting skilla are... (2 Replies)
Discussion started by: sujithchandra
2 Replies

9. AIX

How to find special characters??

By more, vi, cat etc commands special characters (few control characters) are not identified. Is there any way to find out those? Thanks Sumit (3 Replies)
Discussion started by: sumitc
3 Replies

10. Shell Programming and Scripting

Find lines greater than 80 characters in a file

Hi, Can anyone please give me the grep command to find all the lines in a file that exceed 80 columns Thanks, gubbala (8 Replies)
Discussion started by: mrgubbala
8 Replies
Login or Register to Ask a Question