substr() thru awk Korn Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting substr() thru awk Korn Shell Script
# 1  
Old 01-23-2008
substr() thru awk Korn Shell Script

Hi,

I am new stuff to learn substr() function through awk for writing the Korn shell script.

Is there a way to copy from XXXX1234.ABCDEF to XXX1234 file names without changing both data files?

I appreciate your time to response this email.

Thanks,
Steve
# 2  
Old 01-23-2008
I don't understand your question

Are you copying a file named "XXXX1234.ABCDEF" to "XXX1234"? (#1)

Or are you transforming one file to another and transforming the contents from "XXXX1234.ABCDEF" to "XXX1234"? (#2)

And did you mean to have 4 X's in the first and 3 in the second?

If #2 above, must you use korn/awk? Any of python/ruby/perl/sed would do the job as well.
--
Qman
# 3  
Old 01-24-2008
substr() thru awk Korn Shell Script

Hi qneill,

Thanks for your time to review my post.

I knew I am not clear myself because I am not a techie person.

This morning, I reviewed the Similar Thread section and found a solution to separate the file name fields using the awk command.
For example, a file named “XXXX.12345.YYYY” must be changed to “XXXX.12345”.
The echo result of the command line as echo “XXXX.12345.YYYY” | `awk -F. ‘{print $1}'` is “XXXX”. However, I need to use the same command line above for combining the first two fields of the file named “XXXX.12345” Is there a way to show the results of the first two fields of the file name?

Thanks,
sbryant
# 4  
Old 01-24-2008
Code:
echo "XXXX.12345.YYYY" | awk -F. '{print $1"."$2}'

or

Code:
echo "XXXX.12345.YYYY" | awk -F. '{printf("%s.%s", $1, $2)}'

# 5  
Old 01-24-2008
substr() thru awk Korn Shell Script

Perfect! Thanks for your time and help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Substr/Instr in shell script or extract part of text

Hi, I need to extract part of a text to two variables text is "PL/SQL procedure successfully completed. ERROR ----------------------------------------------------------------- Test Error Message PLUSVAR ---------- 1" I want "Test Error Message" in one variable and "1" in another variable.... (11 Replies)
Discussion started by: vedavrath
11 Replies

2. Shell Programming and Scripting

Korn expr substr fails for non-numeric value

I am running AIX 5.3 using the Korn Shell. I am reading file names from a file, as an example: E0801260 E0824349 E0925345 EMPMSTR statement "num=$(expr substr "$DDNAME" 4 2) extracts the numeric values fine. But when I het the last entry, it returns num=MS, but I get an error... (19 Replies)
Discussion started by: kafkaf55
19 Replies

3. Shell Programming and Scripting

substr from a string in Shell script

Shell Scripting Gurus, I am having a hard time :confused: trying to figure out what I am doing wrong in my script. Below is the script snippet. It gives an error when it tries to execute the expression. a=`expr substr $stringZ 5 10` #!/bin/bash echo "Hello" stringZ="abcABC123ABCabc"... (3 Replies)
Discussion started by: yajaykumar
3 Replies

4. Shell Programming and Scripting

How to use substr to return data into a shell script variable?

I'm writing a shell script in which I need to be able to pull a portion of the file name out. I'm testing with the following code: x="O1164885.DAT" y=`ls -ltr *${x}|awk '{print substr($0,3)}'` echo ${x}|awk '{print substr($0,3)}' echo "y="$y I can echo it to the screen just fine but I... (3 Replies)
Discussion started by: ttunell
3 Replies

5. UNIX for Dummies Questions & Answers

Korn shell awk use for updating two files

Hi, I have two text files containing records in following format: file1 format is: name1 age1 nickname1 path1 name2 age2 nickname2 path2 file 1 example is: abcd 13 abcd.13 /home/temp/abcd.13 efgh 15 efgh.15 /home/temp/new/efgh.15 (4 Replies)
Discussion started by: alrinno
4 Replies

6. Shell Programming and Scripting

Could someone give me an example of awk accessing array defined in Korn Shell?

As per title and much apprecieated! (2 Replies)
Discussion started by: biglau
2 Replies

7. Shell Programming and Scripting

Substr in shell script

hi, i am using the following script to get the last digit of YEAR OY=`expr substr $YEAR 2 1` it is showing syntax Is this wrong or i need to change anything I am running this in sun solaris unix (7 Replies)
Discussion started by: gjithin
7 Replies

8. Shell Programming and Scripting

Korn shell and awk question

I am modifying a Korn shell script in using the Exceed (Solaris 10 environment). My task is to read in a .txt file with dates arranged like this (01-Sep-2006). I am to read each line and take the dates, compare them to a benchmark date and depending on if it is older than the date or the date and... (6 Replies)
Discussion started by: mastachef
6 Replies

9. Shell Programming and Scripting

Trouble using substr function with Bourne shell script

Hi, I'm a newbie to UNIX scripting and I'm having some trouble compiling my script. I'm using the Bourne Shell and cannot seem to use the substr function correctly. I'm trying to extract the last two digits of a year that's stored in a variable based off of a condition. I've searched the... (4 Replies)
Discussion started by: E2004
4 Replies

10. Shell Programming and Scripting

AWK question in the KORN shell

Hi, I have two files with the following content: gmrd.txt 235649;03;2563;598 291802;00;2563;598 314634;00;235649;598 235649;03;2563;598 393692;00;2563;598 411805;00;2563;598 411805;00;2563;598 235649;03;2563;598 414037;00;2563;598 575200;00;2563;598 70710;00;2563;598... (11 Replies)
Discussion started by: penfold
11 Replies
Login or Register to Ask a Question