Computing.Net > Forums > Unix > Help with awk

Help with awk

Reply to Message Icon

Original Message
Name: arnettme
Date: May 15, 2007 at 12:10:28 Pacific
Subject: Help with awk
OS: Solaris 9
CPU/Ram: 1024
Model/Manufacturer: Sun Blade 150
Comment:

I'm dumping a string of numbers anywhere between 1-6 figures into a file.

Example 1: cat filea
23754

What I'd like to do is awk the last number of the sting; in this case it would be the "4". There will be only one string of numbers in this file at any given time and I will need only the last number.

Example 2: cat filea
149

In this case, I need the number "9".

I have tried to RTFB, but can't find the answer.

Thanks, Mike


Report Offensive Message For Removal


Response Number 1
Name: ghostdog
Date: May 15, 2007 at 21:17:16 Pacific
Subject: Help with awk
Reply: (edit)

if you want to use awk,
[code]
echo "12345" | awk -F "" '{print NF}'
[/code]
or if you use bash
[code]
# var="12345"
# echo ${var#"${var%?}"}
[/code]


Report Offensive Follow Up For Removal

Response Number 2
Name: vkungwani
Date: May 16, 2007 at 00:54:02 Pacific
Subject: Help with awk
Reply: (edit)

hi ther,

m having a .bat file having a line as below:

cd /apps/bss/BatchProg/bss; ls -l *.sh

How i can i possibly get the customized output like how to use 'cut' or 'awk' with ls???

m running this bat file in COMMAND PROMPT.

Any help will be really appreciated.


Report Offensive Follow Up For Removal

Response Number 3
Name: ghostdog
Date: May 16, 2007 at 02:51:08 Pacific
Subject: Help with awk
Reply: (edit)

i guess the first step is to read the docs?
anyway, i assume you are using unix on windows?
this command: cd /apps/bss/BatchProg/bss; ls -l *.sh can be shortened to
ls -l /apps/bss/BatchProg/bss/*sh.

anyway, describe what you want to get out from your ls -l output. do you want to get file size? time ??


Report Offensive Follow Up For Removal

Response Number 4
Name: arnettme
Date: May 16, 2007 at 10:04:47 Pacific
Subject: Help with awk
Reply: (edit)

Hey ghostdog thanks for the help. You were right with the bash code. It made my job easy thanks.

Mike


Report Offensive Follow Up For Removal

Response Number 5
Name: vkungwani
Date: May 16, 2007 at 16:58:35 Pacific
Subject: Help with awk
Reply: (edit)

Hey ghostdog

actually ls -l iss giving me 5 column output which is permissions, user, file size, date, time, filename. I only want to have file size, date and filename as the output.

I m actually new to this programming...
thanks for your help again.


Report Offensive Follow Up For Removal


Response Number 6
Name: ghostdog
Date: May 16, 2007 at 18:41:25 Pacific
Subject: Help with awk
Reply: (edit)

my ls -l output is different
[code]
ls -l | awk 'NR>1{printf "%s,%s,%s-%s-%s\n",$9,$5,$6,$7,$8}'
[/code]
my col 9 is filename, column 5 is size, column 6-8 is date/time field....substitute for yours as necessary


Report Offensive Follow Up For Removal

Response Number 7
Name: vkungwani
Date: May 17, 2007 at 00:46:30 Pacific
Subject: Help with awk
Reply: (edit)

thanks a lot its working perfectly fine...

just one more question plz

m using the same script

cd /apps/bss/BatchProg/bss; ls -l *.sh > file1.txt

whats the simplest and easiest method to add DATE stamp on the filename i.e. file120070517.txt

i tried this :

file1%month%%day%%year% , it worked yesterday but for some reason its not working anymore and i m pretty sure i didnt delete anything.

can u help me with this as well plz

looking forward for a reply soon



Report Offensive Follow Up For Removal

Response Number 8
Name: ghostdog
Date: May 17, 2007 at 01:11:30 Pacific
Subject: Help with awk
Reply: (edit)

are you using a combination of batch + shell scripting? can you describe your environment? i guess if you can use ls, cd, and those normal shell commands , you can also use the unix date utitlity. without further info, i can only show you the how i do it in Unix.
[code]
thedate=$(date "+%Y%m%d")
mv "$filename" "$filename$thedate"
[/code]


Report Offensive Follow Up For Removal

Response Number 9
Name: vkungwani
Date: May 17, 2007 at 17:21:56 Pacific
Subject: Help with awk
Reply: (edit)

m using a batch script in command prompt. And m diaplaying the output from a remote server. Finally m storing the output in a txt file with the date on it. So that later on I can compare the files and get to know the daily changes in the server.

the actual and complete script is :

plink -ssh -pw XXXXXXXXX sr54221@nus730pn cd /apps/bss/BatchProg/bss; ls -l *.sh >> nus730pn%(date "+%Y%m%d");


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Help with awk

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software