Remove prefix per line in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove prefix per line in file
# 1  
Old 03-22-2010
Remove prefix per line in file

Hi,

I'm using a .ksh script to split one file into multible files by checking for the prefix per line. It works perfekt (thanks again for anyone involved in helping me with that Smilie), but I want to remove the prefix per line too. Means only the line information itself should remain in the splitted files.

Here the code used to do the split, depending on filename, and creating variables with the filename for every file created:

Code:
if [[ ${_ftpfile} = @(ORD*) || ${_ftpfile} = @(SHP*) ]]; then
awk '{close(f);f=$1}{print > f".TXT"}' $_ftpfile
set B*.TXT
i=1
c=$#
x=$((c+1))
echo "$x"
while [[ $x -gt $i ]] ; do
eval _ftpfile$i="$"$i
i=$((i+1))
done
backup_files
fi

A line in a file then looks like:

BDD000000928 00000681 DB00000681 419999 7 0020302

How can I modify my awk to remove the BDD prefix? The lines then should look like:

000000928 00000681 DB00000681 419999 7 0020302

Thanks!
# 2  
Old 03-22-2010
Code:
awk '{close(f);f=$1}{sub("BDD","");print > f".TXT"}' $_ftpfile


Last edited by anbu23; 03-22-2010 at 12:18 PM..
# 3  
Old 03-22-2010
ok thanks so far ... forgot to mention that I have 4 files with 4 different prefixes per line Smilie

so how about different prefixes, like BDD, BHD, BAD, BND?
# 4  
Old 03-22-2010
Quote:
Originally Posted by spidermike
ok thanks so far ... forgot to mention that I have 4 files with 4 different prefixes per line Smilie

so how about different prefixes, like BDD, BHD, BAD, BND?
all those starts with "B" n ends with "D" so might this work?

Code:
awk '{close(f);f=$1}{sub("^B[A-Za-z]D","");print > f".TXT"}' $_ftpfile

# 5  
Old 03-22-2010
In this case yes, but as it should work with different files, which might then have other prefixes, it should more be a generic thing like to always cut off the first 3 chars per line. That would help best ...
# 6  
Old 03-22-2010
Quote:
Originally Posted by spidermike
In this case yes, but as it should work with different files, which might then have other prefixes, it should more be a generic thing like to always cut off the first 3 chars per line. That would help best ...
Code:
awk '{close(f);f=$1}{sub("^[A-Z][A-Z][A-Z]","");print > f".TXT"}' $_ftpfile

this should work, u can use SED too using only a dot instead of each [A-Z].
# 7  
Old 03-22-2010
Code:
sed 's/^\(...\)\(.*\)/echo "\2" >>\1.file/' file | sh

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove first line of file

How can I use bash to remove the first line of a file? (3 Replies)
Discussion started by: locoroco
3 Replies

2. Shell Programming and Scripting

Remove line from file and read file line by line

I have a file output.txt. File looks like this name1 10 name2 2 name3 5 I get a number n and I need to remove all lines which has number (after name) smaller or equal to n number. After that I need to write lines from file and my output must be like this: Output: 'name1 10' Output: 'name2... (1 Reply)
Discussion started by: kubo12312
1 Replies

3. Shell Programming and Scripting

Extract Uniq prefix from a start and end prefix

Dear All, assume i have a file with content: <Start>6000</Start> <Stop>7599</Stop> the output is: 6000 7000 7100 7200 7300 7400 7599 how should we use any awk, sed, perl can do this task, means to extract the uniq prefixes from the start and stop prefix. Thanks Jimmy (3 Replies)
Discussion started by: jimmy_y
3 Replies

4. Shell Programming and Scripting

Need awk script to add a prefix to each line in file

Hello , I have file with below content : '165567885', '165568443', '165568805', I need an awk script that would add a prefix zero after first ' . Like '0165567885', '0165568443', '0165568805', Please help. Thanks in advance. (5 Replies)
Discussion started by: rmv
5 Replies

5. Shell Programming and Scripting

Remove prefix using awk

Remove prefix using awk File: nawk -F"|" '{if ($1 ~ /^xyz./) print; else { gsub(.*\..*, \..*, $1) ;print }}' file Error: ouput required: (5 Replies)
Discussion started by: pinnacle
5 Replies

6. Shell Programming and Scripting

Remove header(first line) and trailer(last line) in ANY given file

Hi, I need some help in removing the header (first line) and the trailer (last line) in a give file... The data file actually comes in EBCDIC format and I converted it into ASCII.. Now I need to strip off the first line and the last line.. I think we can use sed to do something like this:... (2 Replies)
Discussion started by: madhunk
2 Replies

7. Shell Programming and Scripting

Remove every third line from a file

I need to remove every second and every third line from a file. My idea was to do it in two operations. First every third line, then every second line. The problem is that i can't find out how to do it. I tried to look for some sed oneliners, but couldn't find any. Suggestions? (4 Replies)
Discussion started by: bistru
4 Replies

8. Shell Programming and Scripting

Remove prefix from filenames

I'm trying to put together a shell script that will append specific prefixes based on the content of filenames. I think I have this part down. However, I want to append before that part a process that will remove the current prefix before it renames the files with the new prefix. For example,... (6 Replies)
Discussion started by: HLee1981
6 Replies

9. UNIX for Dummies Questions & Answers

remove filename prefix

I've got a bunch of files called oldabc, olddef etc. i want to copy these to be abc, def.... I can do this with file extensions....but can get the logic to work for prefixes. All the files I am interested in have a prefix of 'old'. This loop is no good for me....it looks at the content... (2 Replies)
Discussion started by: peter.herlihy
2 Replies
Login or Register to Ask a Question