How to call last 14 characters with grep/sed in shell script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to call last 14 characters with grep/sed in shell script.
# 1  
Old 06-08-2009
How to call last 14 characters with grep/sed in shell script.

Hi. This is my first post on the forums.

I am trying to write a script that will parse a folder of files "oneverylongfilenamexyz.pdf" and create a .dat file named "oneverylongfilenamexyz.dat" with the first line of each .dat file saying variable="xyz" where xyz is the last 14 characters of $i before the extension.

My syntax is very poor but I think the logic should be something like

# first loop to make dat files
for i in `ls -R dat_tmp/ | grep .pdf`
touch $i.dat
# second loop to fill dat files
for i in `ls -l | grep .dat`
echo 'variable=`sed[last 14 characters] $i`' > $i.dat


Any help is much appreciated. Thank you.

Last edited by attonbitusira; 06-08-2009 at 05:12 PM.. Reason: made some progress
# 2  
Old 06-08-2009
I have few doubts if you want .pdf files thaen why you used grep -v??
and ls -l won't give only filename..
to take last 14 char of filename(I think thats what you meant)
use this in you second for loop
Code:
basename $i .dat |cut -c 14-

# 3  
Old 06-08-2009
Yes, I realized my error and changed it.

Thanks for the suggestion. Basename I believe will work.
# 4  
Old 06-08-2009
this will also do
Code:
echo "oneverylongfilenamexyz.dat"|awk -F"." '{print substr($1,length($1)-14)}'

# 5  
Old 06-08-2009
Finished script. Thanks for the help!

Code:
# first loop to make dat files
for i in `ls -R $dattmp | grep .pdf`
do
touch $dattmp/`basename $i .pdf`.dat
done
# second loop to fill dat files
for i in `ls $dattmp/ | grep .dat`
do
echo trans_num=`basename $i .dat | cut -c 7-20` > $dattmp/`basename $i .dat`.dat
done
mv $dattmp/* $blade6path/

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed or awk grep, that will only get the line with more characters.

Is there a command for sed and awk that will only sort the line with more characters? #cat file 123 12345 12 asdgjljhhho bac ss Output: asdgjljhhho #cat file2 11.2 12345.00 21.222 12345678.10 (2 Replies)
Discussion started by: invinzin21
2 Replies

2. Shell Programming and Scripting

Shell script to call Oracle archive backup script when file system reaches threshold value

Hello All, I need immediate help in creating shell script to call archivebkup.ksh script when archive file system capacity reaches threshold value or 60% Need to identify the unique file system that reaches threshold value. ex: capacity ... (4 Replies)
Discussion started by: sasikanthdba
4 Replies

3. Shell Programming and Scripting

SED equivalent for grep -w -f with pattern having special characters

I'm looking for SED equivalent for grep -w -f. All I want is to search a list of patterns from a file. Also If the pattern doesn't match I do not want "null returned", rather I would prefer some text as place holder say "BLANK LINE" as I intend to process the output file based on line number. ... (1 Reply)
Discussion started by: novice_man
1 Replies

4. Red Hat

how to call a particular function from one shell another shell script

please help me in this script shell script :1 *********** >cat file1.sh #!/bin/bash echo "this is first file" function var() { a=10 b=11 } function var_1() { c=12 d=13 (2 Replies)
Discussion started by: ponmuthu
2 Replies

5. Shell Programming and Scripting

call another shell script and pass parameters to that shell script

Hi, I basically have 2 shell scripts. One is a shell script will get the variable value from the user. The variable is nothing but the IP of the remote system. Another shell script is a script that does the job of connecting to the remote system using ssh. This uses a expect utility in turn. ... (2 Replies)
Discussion started by: sunrexstar
2 Replies

6. Shell Programming and Scripting

grep or sed. How to remove certain characters

Here is my problem. I have a list of phone numbers that I want to use only the last 4 digits as PINs for something I am working on. I have all the numbers in a file but now I want to be removed all items EXCEPT the last 4 digits. I have seen sed commands and some grep commands but I am... (10 Replies)
Discussion started by: Sucio
10 Replies

7. UNIX for Dummies Questions & Answers

Escaping non-readable characters using grep, sed or awk

I'm trying to parse out DNS logs from dozens of different domain controllers over a large period of time. The logs are rolled up into individual text files by size, which may contain only a portion of a day's activity or several day's worth (depending on amount of activity). I'm splitting them by... (4 Replies)
Discussion started by: seanwpaul
4 Replies

8. Shell Programming and Scripting

Need shell/sed script for grep+string replacement

Hi, Let me explain the situation. There are many files in a directory and its sub-directories that conatin the string pattern "pa". I want to replace all such instances with the pattern "pranavagarwal" doing a grep "pa" `ls` does give me all the instances of the occurence of that... (3 Replies)
Discussion started by: pranavagarwal
3 Replies

9. UNIX for Dummies Questions & Answers

a system call for sed in a awk script

Hi, this is my test file : DELETE FROM TABLE WHERE ID_INTERNAL = :TABLE.ID-INTERNAL, ID-INTERNAL-CRAZY ID-INTERNAL-OPEN ID-INTERNAL /ID-INTERNAL/ I want all occurences of ID-INTERNAL replaced with a one, if ID-INTERNAL has and dash afer it , dont replace it example:... (6 Replies)
Discussion started by: seaten
6 Replies

10. Shell Programming and Scripting

grep, sed in a shell script

Hi, I have a problem with a simple script I am trying to write. I want a user to type grep, sed commands that are then stored in variables. Those variables are stored in a function, and the function is then called to execute the commands. The idea is that the user does it step by step. ... (4 Replies)
Discussion started by: Trufla
4 Replies
Login or Register to Ask a Question