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

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:

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

KR,
Eagle

Try:

j=./abc/20141127
echo "${j#.}"
1 Like

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 dir variable.

Oki Let me provide you more detailed input so that it could give you a clue what im trying to do:

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:

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:

/data/sdp/omm/log/abm/20141127
000029abm_20141127031628.txt:a|b1|c3|4|55|x|y|z
000030abm_20141127031638.txt:t|u1|k3|m4

To get at the absolute paths of the directories in question,

find $(pwd) -type d -name "$(date +%Y%m%d)"

; to list the files in there try

find $(pwd) -type d -print -exec ls -t1 {} \; 

; to get the most recent ones try

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.