Visit Our UNIX and Linux User Community


AWK how to strip from right hand side


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK how to strip from right hand side
# 1  
Old 01-16-2008
AWK how to strip from right hand side

guys,
i am writing a .ksh file to ssh to a remote machine and change all occurances of .ixf to .WIP like this :

-->>> for i in *.ixf do echo $i done mv $i $i.WIP exit <<---

--> this returns .ixf.WIP - i can live with that.

then i need to sftp from another remote machine, copy the files across to the new box and rename the file back to .ixf like this :

-->> for i in *.WIP; do echo $i; done; mv $i $i.ixf <<--

--> this returns .ixf.WIP.ixf - i can't live with this.

I need to strip WIP.ixf from the end, i think using awk but cannot get the syntax correct.
Any help here would be appreciated.
Thanks
# 2  
Old 01-16-2008
use basename

I assume this is a unix box...
awk is a nuclear bomb, when all you need is a hammer.

Use basename (see example below)

#!/bin/ksh
for i in *.ixf; do
name=$(basename $i .ixf)
echo $name
mv $i $name.WIP
done
# 3  
Old 01-16-2008
If using 'sed ' is an option:
Code:
for i in *.WIP; do 
echo $i 
mv $i `echo $i | sed 's/\..*$//'`.ixf
done

Or if you want (GNU) awk, and assuming the 1st dot in the filename separates all the extension(s):
Code:
for i in *.WIP; do 
echo $i 
mv $i `echo $i | gawk -F . '{print $1}'`.ixf
done

HTH,
Zsoltik@
# 4  
Old 01-16-2008
Quote:
Originally Posted by angelolamberti
guys,
i am writing a .ksh file to ssh to a remote machine and change all occurances of .ixf to .WIP like this :

-->>> for i in *.ixf do echo $i done mv $i $i.WIP exit <<---

--> this returns .ixf.WIP - i can live with that.

then i need to sftp from another remote machine, copy the files across to the new box and rename the file back to .ixf like this :

-->> for i in *.WIP; do echo $i; done; mv $i $i.ixf <<--

--> this returns .ixf.WIP.ixf - i can't live with this.

I need to strip WIP.ixf from the end, i think using awk but cannot get the syntax correct.
Any help here would be appreciated.
Thanks
Code:
for i in *.WIP
do
  echo $i
  OUTFILE=$(echo ${i%(\.[A-Z]*)}
  mv $i $OUTFILE
done

# 5  
Old 01-16-2008
Quote:
Originally Posted by zsoltika
If using 'sed ' is an option:
Code:
for i in *.WIP; do 
echo $i 
mv $i `echo $i | sed 's/\..*$//'`.ixf
done

He need to remove the extension.

Code:
for i in *.WIP
do 
  mv $i `echo $i | sed 's/\.WIP$//'`
done

# 6  
Old 01-17-2008
Quote:
Originally Posted by n1djs
I assume this is a unix box...
awk is a nuclear bomb, when all you need is a hammer.

Use basename (see example below)

#!/bin/ksh
for i in *.ixf; do
name=$(basename $i .ixf)
echo $name
mv $i $name.WIP
done


superb, thanks

Previous Thread | Next Thread
Test Your Knowledge in Computers #500
Difficulty: Easy
Comments in code provide a high-level description of what a block of code, function, or program does.
True or False?

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching number of syllables on right-hand and left side

I am developing a database for translating names. I have mapped through a rule engine syllables in English to syllables in Indic, delimited by an equal to sign. An example will illustrate this ra m=रा म ku ma r=कु मा र mo=मो la l=ला ल gi ta=गी ता ka la va ti=कa ला वa ती However it so... (3 Replies)
Discussion started by: gimley
3 Replies

2. Shell Programming and Scripting

Merge left hand strings mapping to different right hand strings

Hello, I am working on an Urdu to Hindi dictionary which has the following structure: a=b a=c n=d n=q and so on. i.e. Headword separated from gloss by a = I am giving below a live sample بتا=बता بتا=बित्ता بتا=बुत्ता بتان=बतान بتان=बितान بتانا=बिताना I need the following... (3 Replies)
Discussion started by: gimley
3 Replies

3. Shell Programming and Scripting

AWK to merge multiple files side by side

I have about 100s of files of type text in a known directory. I want to merge all files side by side. Number of lines in all the files will remain same. For example file1 contains cat dog File 2 contains rat mat Output file should be cat rat dog mat Using awk I was able to... (5 Replies)
Discussion started by: kanthrajgowda
5 Replies

4. Shell Programming and Scripting

printing 3 files side by side based on similar values in rows

Hi I'm trying to compare 3 or more files based on similar values and outputting them into 3 columns. For example: file1 ABC DEF GHI file2 DEF DER file3 ABC DER The output should come out like this file1 file2 file3 ABC ABC (4 Replies)
Discussion started by: zerofire123
4 Replies

5. Shell Programming and Scripting

Paste two file side by side together based on specific pattern match problem

Input file_1: P78811 P40108 O17861 Q6NTW1 P40986 Q6PBK1 P38264 Q6PBK1 Q9CZ49 Q1GZI0 Input file_2: (6 Replies)
Discussion started by: patrick87
6 Replies

6. Web Development

Cannot access Apache web server from Wan side, only Lan side.

I have installed WAMPSERVER 2.0 on my windows vista x64 system but still am having issues with getting the webserver to be seen outside my local network. It is working fine within my local network. Been through several setup tutorials so far, no dice still. For testing purposes I have... (1 Reply)
Discussion started by: davidmanvell
1 Replies

7. Shell Programming and Scripting

How to Merge / combine / join / paste 2 text files side-by-side

I have 2 text files, both have one simple, single column. The 2 files might be the same length, or might not, and if not, it's unknown which one would be longer. For this example, file1 is longer: ---file1 Joe Bob Mary Sally Fred Elmer David ---file2 Tomato House Car... (3 Replies)
Discussion started by: cajunfries
3 Replies

8. Shell Programming and Scripting

Script to place selected columns from a group of files side by side in a new file

Hi Everyone, I need a shell/perl script to bring selected columns from all the files located in a directory and place them in a new file side by side. File1: a b c d 2 3 4 5 f g h i .......... File2: I II III IV w x y z .............. and so on many files are there...... (8 Replies)
Discussion started by: ks_reddy
8 Replies

Featured Tech Videos