Assign awk gsub result to a variable


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assign awk gsub result to a variable
# 1  
Assign awk gsub result to a variable

Hello,

I have searched but failed to find what exactly im looking for,

I need to eliminate first "." in a output so i can use something like the following

Code:
echo "./abc/20141127" | nawk '{gsub("^.","");print}'

what i want is to use gsub result later on, how could i achieve it?

Let say i have a "$j" variable coming from a for-do loop (its a directory) and im using this variable in nawk by assinging it to dir, but i want to use it first stripping "." at the beginning. something like the following but i know red highlighted "dir" does not do what i want:

Code:
nawk -v dir=$j '!/total/{gsub("^.","",dir)}{print str"/"$NF}'

KR,
Eagle
# 2  
Try:
Code:
j=./abc/20141127
echo "${j#.}"

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Sorry? More context, please.
Some ad hoc comments:
Escape the dot wildcard char to really drop the leading "." only.
gsub on an anchored regex doesn't make sense (although it doesn't hurt); there's only one match.
Repeatedly applying gsub("^.","",dir) will eventually (= after a few lines) empty the dirvariable.
# 4  
Oki Let me provide you more detailed input so that it could give you a clue what im trying to do:

Code:
bash-3.00$ for j in $(find . -type d -name "`date +%Y%m%d`" 2>/dev/null);do echo $j ;done
./output/fail/20141127
./output/20141127
./abm/20141127
./so/20141127
./error/20141127
./error/suspension/20141127
./ccn/20141127
./input/20141127

There are several files under those folders above, and in those files there are several rows (records) too.

By using a one liner (otherwise i need complicated a scrpit file to write) I would like to print these daily created folders and their last 2 files under like this:
Code:
bash-3.00$ for j in $(find . -type d -name "`date +%Y%m%d`" 2>/dev/null);do echo -e "\n`pwd`${j#.}" && ls "`pwd`${j#.}" | tail -2;done

/data/sdp/omm/log/output/fail/20141127

/data/sdp/omm/log/output/20141127
000248outputparams_20141127111523.txt
000249outputparams_20141127111553.txt

/data/sdp/omm/log/abm/20141127
000029abm_20141127031628.txt
000030abm_20141127031638.txt

It could be very nice to print the context of each file next to the file name
context of those txt files are pipe seperated such as "a|b1|c3|4|55|x|y|z"

ideal output would be something like:
Code:
/data/sdp/omm/log/abm/20141127
000029abm_20141127031628.txt:a|b1|c3|4|55|x|y|z
000030abm_20141127031638.txt:t|u1|k3|m4

# 5  
To get at the absolute paths of the directories in question,
Code:
find $(pwd) -type d -name "$(date +%Y%m%d)"

; to list the files in there try
Code:
find $(pwd) -type d -print -exec ls -t1 {} \;

; to get the most recent ones try
Code:
find $(pwd) -type d -print -exec ls -t1 {} \; | grep -A2 "^/"

This is not bullet proof; e.g. directories with less than two files will mess up.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #62
Difficulty: Easy
DNS in Internet terms stands for the 'Domain Name System'.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove a character and assign result to a variable

I am reading lines from a file that contain a number sign (#) before a three or four digit number: #1043 #677 I can remove the '#' and get just the number. However, I then want to assign that number to a variable and use it as part of a path further on in my program: /mydir/10/1043 for... (5 Replies)
Discussion started by: KathyB148
5 Replies

2. Shell Programming and Scripting

Assign the result of a multiline command to a variable

Hi, I have the following command that lists all the .o files from all the directories except of vwin (which I don't want it) for i in `ls -d */*.o|awk '$0 !~ "vwin"'`; do echo $i; done The result is something like that dir1/file1.o dir1/file2.o dir2/file3.o etc. So, I want to create a... (9 Replies)
Discussion started by: scor6800
9 Replies

3. Shell Programming and Scripting

Trouble with passing Variable from bash to awk gsub command

Would really appreciate it if someone could point out my mistake in this line of code, i've been staring blankly at it trying everything i can think of some time now and coming up with nothing. #!/bin/bash echo "Enter Username" read Username awk -F: -v var=${Username} '/^var:/... (9 Replies)
Discussion started by: Nostyx
9 Replies

4. UNIX for Dummies Questions & Answers

Assign SQL result in shell variable

Hi im trying to assign the result of the db2 command to a variable inside a shell script... : tab_cnt=`db2 "select count(*) from syscat.tables where tabname = 'ABC' and tabschema = 'MATT01'" |head -4|tail +4|cut -c 11` : echo $tab_cnt when i echo im getting a blank value.. im expecting... (1 Reply)
Discussion started by: matt01
1 Replies

5. Shell Programming and Scripting

Assign zero to strings that don't appear in block, store result in AWK array

Hi to all, I have this input: <group> <x "2">Group D</x> <x "3">Group B</x> <x "1">Group A</x> </group> <group> <x "1">Group E</x> <x "0">Group B</x> <x "1">Group C</x> </group> <group> ... (11 Replies)
Discussion started by: Ophiuchus
11 Replies

6. Shell Programming and Scripting

awk's gsub variable in replacement

I been trying to figure out how to use element of array as a replacement pattern. This works as I expected: $ echo "one two three" | awk '{ gsub(/wo/,"_BEG_&_END_",$2); print }' one t_BEG_wo_END_ three $ echo "one two three" | awk '{ tmp="foo"; gsub(/wo/,"_BEG_" tmp "_END_",$2);... (5 Replies)
Discussion started by: mirni
5 Replies

7. Shell Programming and Scripting

Assign result to variable

Hi friends, firstly, i can run following expression and i took 100 value. sqlplus -s username/password@TTTEST @umt.sql umt.sql exists "select t.deger from parametre t where t.id=30". result of this query =100 i need to assign this value(100) to variable(for example x... (2 Replies)
Discussion started by: temhem
2 Replies

8. Shell Programming and Scripting

How to assign the result of a SQL command to more than one variable in shell script.

Hi Friends... Please assist me to assign the result of a SQL query that results two column, to two variables. Pls find the below code that I write for assigning one column to one variable. and please correct if anything wrong.. #! /bin/sh no=' sqlplus -s uname/password@DBname... (4 Replies)
Discussion started by: little_wonder
4 Replies

9. Shell Programming and Scripting

assign awk command result to a variable

#!/bin/sh # ## MYSTRING = `awk '/myApp.app/' /Users/$USER/Library/Preferences/loginwindow.plist` if then echo String not found defaults write /Users/$USER/Library/Preferences/loginwindow AutoLaunchedApplicationDictionary -dict-add -string Hide -bool YES -string Path -string... (9 Replies)
Discussion started by: dedmakar
9 Replies

10. Shell Programming and Scripting

assign subst|grep|sed command result to a variable

Hi, I'm quite new to scripting and I want to modify following line of an existing script: MYVAR=`subst |grep 'L:\\\:' | sed -e 's/.*\\\//'`; What I have to do is to use the content of a variable instead of the constant expression 'L:\\\:' as the grep string to be matched. Assuming I already... (5 Replies)
Discussion started by: snowbiker99
5 Replies

Featured Tech Videos