Variable%pattern operation within awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable%pattern operation within awk
# 1  
Old 01-04-2012
Variable%pattern operation within awk

Hello;

I have:
Code:
ll | grep -v ^d | awk '{print $9}'
rcx_access_report_fid.txt
rcx_access_report_hsi.txt
rcx_access_report_mmm.txt
rcx_access_report_qsc.txt

I want to get:

Code:
rcx_access_report_fid.txt
rcx_access_report_hsi
rcx_access_report_mmm
rcx_access_report_qsc

But when I try:

Code:
ll | grep -v ^d | awk '{print ${9%.*}}'
awk: cmd. line:1: {print ${9%.*}}
awk: cmd. line:1:         ^ syntax error
awk: cmd. line:1: {print ${9%.*}}
awk: cmd. line:1:               ^ syntax error

Is there a way to do this within awk ??

Thank you
# 2  
Old 01-04-2012
YMMV:
Code:
echo 'rcx_access_report_fid.txt' | nawk -F. '{print $1}'

OR more generically:
Code:
echo 'rcx_access_report_fid.txt' | nawk '
  function rindex(str,c)
   {
     return match(str,"\\" c "[^\\" c "]*$")? RSTART : 0
   }
   {print substr($0,1,rindex($0,".")-1)}'

# 3  
Old 01-04-2012
Not to remove .txt in first line !!?? If yes, try with the below ..
Code:
$ ll | grep -v ^d | nawk '{if(NR!=1){sub(/.txt/,"",$9);print $9}else{print $9}}'
rcx_access_report_fid.txt
rcx_access_report_fid
rcx_access_report_fid
rcx_access_report_fid


Last edited by jayan_jay; 01-04-2012 at 10:02 AM..
# 4  
Old 01-04-2012
More generic (and saving the grep pipe)

Code:
ll| awk '/^[^d]/{x=$9;y=(!sub("\.[^\.]*","",$9))?x:(++c==1)?x:$9;print y}'


Last edited by ctsgnb; 01-04-2012 at 09:39 AM..
This User Gave Thanks to ctsgnb For This Post:
# 5  
Old 01-04-2012
Quote:
Originally Posted by jayan_jay
Not to remove .txt in first line !!?? If yes, try with the below ..
Sorry that was my typo ..
I wanted to delete all file extenxsions

Thnx again
# 6  
Old 01-04-2012
Perl.
Code:
ll | perl -ne 'if(!/^d/){s/(.*)\..*/$1/; print}'

This will display "abc.pqr" if the filename is "abc.pqr.xyz"
# 7  
Old 01-05-2012
Quote:
Originally Posted by delphys
Sorry that was my typo ..
I wanted to delete all file extenxsions

Thnx again
Code:
$ ll | grep -v ^d | nawk '{sub(/.txt/,"",$9); print $9}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk -Search pattern through Variable

Hello, We have wrote shell script for multiple file name search pattern. file format: <numner>_<20180809>.txt starting with single number and ending with 8 digits number Command: awk -v string="12_1234" -v serch="^+_+$" "BEGIN{ if (string ~/serch$/) print string }" If sting matches... (4 Replies)
Discussion started by: koti_rama
4 Replies

2. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

3. Shell Programming and Scripting

Arithmetic operation in variable

Hi, Here is the script i try to perform arithmetic operation in two variables . git branch -r | while read brname ; do REV_COMMITS=`git rev-list --count $brname` echo "$brname has $REV_COMMITS" (( TOTAL = TOTAL + REV_COMMITS )) echo "in loop" $TOTAL done echo "total is " $TOTAL ... (3 Replies)
Discussion started by: greet_sed
3 Replies

4. Programming

take input from a variable as pattern to awk

Hi everyone, Can anyone tell me how to take contents of a variable as a pattern for awk command. Am doing as below, but doesnt get any output: $c = "Tue Dec"; $log = ` awk '/ \$c /' in.txt`; print $log; (7 Replies)
Discussion started by: anandrec
7 Replies

5. Shell Programming and Scripting

how to Declare 5 values to one variable with OR operation

what I'm trying to do is ... need to drop tables w/ names like ABC_NY_2001 ABC_ORD_2001 ABC_TX_2001 ABC_CL_2001 For this, I want to write a query "DROP TABLE ABC_var_2001". now "var" should be either NY, ORD, TX or CL. I'm new to programming so don't know how to create a variable w/ OR... (3 Replies)
Discussion started by: ramsowji
3 Replies

6. Shell Programming and Scripting

populate a bash variable from an awk operation

Hi, I'm trying to populate bash script variable, data_size with the size of the largest file in my current directory data_size=$(ls -lS | grep -v "total" | head -1) | awk '{ print $5 }' I've tried adding an echo before the call to awk data_size=$(ls -l | grep -v "total" | head -1) |... (2 Replies)
Discussion started by: mark_s_g
2 Replies

7. Shell Programming and Scripting

Insert external variable in a AWK pattern

Dear all, ¿How can i insert a variable in a AWK pattern? I have almost succeeded in solving a puzzle with AWK but now i want to make a script. Let me explain. cat file.txt | awk 'BEGIN {RS="\\n\\n"} /tux/ { print "\n"$0 }' I know that this command makes right what i want to do, but mi... (8 Replies)
Discussion started by: antuan
8 Replies

8. Shell Programming and Scripting

Search for awk pattern in unix env variable

For a Script I need to detemine which field of the unix environment variable SHLIB_PATH has the WALTDB entry. export SHLIB_PATH=/usr/user5/WALTDB/oracle/product/10.2.0/lib32:/usr/TZD/bin.wdad/mug/oracle/lib: echo $SHLIB_PATH | awk -F: '{ print $1 }' Shure gives me the first entry, but... (6 Replies)
Discussion started by: sdohn
6 Replies

9. Shell Programming and Scripting

AWK: pattern not properly stored in variable?

Hey there, I have a table of contents file of the form 1 Title1 1.1 Subtitle1 1.1.1 Subsubtitle1 1.1.2 Subsubtitle2 ... and want to count the number of dots in the first field to find out the level of the section. I use the gsub function for the job, which works if I pass the pattern... (2 Replies)
Discussion started by: herrsimon
2 Replies

10. Shell Programming and Scripting

Variable Selection Operation Error

Ok i have taken your advised indented my code and i have managed to fix my problem but unfortuantely now another small one has arisen. The problem is that executing my commands requires two presses of the ENTER key as opposed to the originally being pressed once as one would expect, for example... (1 Reply)
Discussion started by: warlock129
1 Replies
Login or Register to Ask a Question