Using of gsub function in AWK to replace space by underscore


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using of gsub function in AWK to replace space by underscore
# 1  
Old 07-15-2011
Using of gsub function in AWK to replace space by underscore

I must design a UNIX script to monitor files whose size is over a threshold of 5 MB in a specific UNIX directory

I meet a problem during the for loop in my script. Some file names contain spaces.

ls -lrt | awk '$5>=5000000 && length($8)==5 {gsub(/ /,"_",$9); print};'

-rw-r--r-- 1 was61 web 19488100 Jan 17 11:13 BNP IDOCS 20110117 1058.TXT.1295262807802.239
-rw-r--r-- 1 was61 web 9187328 Jan 20 06:07 CHEQUES XP ES70.MDB.1295503676684.1665
-rw-r--r-- 1 was61 web 16968480 Jan 24 12:55 BNP IDOCS 20110124 1058.TXT.1295873751733.325
-rw-r--r-- 1 was61 web 17229240 Jan 31 12:22 BNP IDOCS 20110131 1058.TXT.1296476552934.3663
-rw-r--r-- 1 was61 web 13678240 Feb 7 14:02 BNP IDOCS 20110207 1058.TXT.1297087373504.377
-rw-r--r-- 1 was61 web 15373180 Feb 14 12:59 BNP IDOCS 20110214 1058.TXT.1297688349015.250
-rw-r--r-- 1 was61 web 15743120 Feb 21 13:03 BNP IDOCS 20110221 1058.TXT.1298293391952.390
-rw-r--r-- 1 was61 web 16881560 Feb 28 12:26 BNP IDOCS 20110228 1058.TXT.1298895983632.4110
-rw-r--r-- 1 was61 web 9494528 Mar 2 06:01 CHEQUES XP ES04.MDB.1299045692115.605
-rw-r--r-- 1 was61 web 15790820 Mar 7 13:21 BNP IDOCS 20110307 1058.TXT.1299504116925.293
-rw-r--r-- 1 was61 web 17004520 Mar 14 12:45 BNP IDOCS 20110314 1058.TXT.1300106758008.320
-rw-r--r-- 1 was61 web 18494880 Mar 21 13:22 BNP IDOCS 20110321 1058.TXT.1300713733016.415
-rw-r--r-- 1 was61 web 17444420 Mar 28 11:22 BNP IDOCS 20110328 1058.TXT.1301311351565.3013
-rw-r--r-- 1 was61 web 18307260 Apr 4 11:30 BNP IDOCS 20110404 1058.TXT.1301916643739.384
-rw-r--r-- 1 was61 web 16373820 Apr 11 12:07 BNP IDOCS 20110411 1058.TXT.1302523652962.365
-rw-r--r-- 1 was61 web 17351140 Apr 18 12:08 BNP IDOCS 20110418 1058.TXT.1303128529028.304
-rw-r--r-- 1 was61 web 6560656 May 6 11:25 IDOC20110509.1304681132500.3221
-rw-r--r-- 1 was61 web 40534016 May 13 15:30 EBW6.MDB.1305300617379
-rw-r--r-- 1 was61 web 8710871 May 26 06:47 IDOC20110526.1306392435096.870
-rw-r--r-- 1 was61 web 5061366 May 27 13:24 DRX_BNP_PRL_M_2705111400_4.TXT.1306502691153.2079
-rw-r--r-- 1 was61 web 7060706 Jun 23 13:12 IDOC20110624.1308834730309.5850
-rw-r--r-- 1 was61 web 5127367 Jun 26 06:00 CFONB160_DDA_COLLECTION.B0703480.1309068056116.8
-rw-r--r-- 1 was61 web 7766712 Jun 30 15:55 KSA050711.TXT.1309449312795.3334
-rw-r--r-- 1 was61 web 20977043 Jul 8 13:17 EXMPGSM.CSV.1310131067933.3590
-rw-r--r-- 1 was61 web 20977043 Jul 8 13:19 EXMPGSM.CSV.1310131156554.3591
-rw-r--r-- 1 was61 web 5440544 Jul 13 14:18 IDOC20110715.1310566725298.2316


When I execute ls -lrt |awk '$5>=5000000 {print $9}', file names contain spaces and are considered as different inputs in the for loop.

Unfortunately, file names are handled by external customers so I must consider it as a constraint.

To avoid such a situation, I would like to replace spaces by underscores or any other character.

In spite of my efforts, I do not succeed in replacing these spaces with gsub function in AWK.
I tried this -> ls -lrt |awk '$5>=5000000 {gsub(/ /, "_", $9); print $9}'

Unfortunately, the result of my AWK command is identical to the one without gsub function.
Just for precisions, I am on a korn shell in AIX 5.3 version.

How should I setup this gsub function in AWK ? Are ther other solutions to find a workaround to this problem ?

Thanks by advance for your feedback.
# 2  
Old 07-15-2011
Code:
 
ls -lrt | awk '$5>=5000000 && length($8)==5 { for(i=9;i<=NF;i++){ if(i==NF) {printf("%s\n",$i);} else{printf("%s_",$i)}}}'

---------- Post updated at 01:40 PM ---------- Previous update was at 01:37 PM ----------

Code:
 
find . -type f -size 5000 -maxdepth 1

# 3  
Old 07-15-2011
Thanks a lot, it works !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a space with underscore in a file

i have a file with following data. { EqName "Tan 1" .... .... } { EqName "Sin 2" ... ... } I have to replace the value of EqName to Tan_1 and Sin_2 in file.Can i use sed or awk ? cat file|grep EqName|awk '{print $2 $3}'|sed -i 's//_/g' I tried with this but it... (2 Replies)
Discussion started by: Jag02
2 Replies

2. Shell Programming and Scripting

awk gsub command to replace multiple spaces

Hi Forum. I'm trying to cleanup the following data elements (To remove any occurences of commas and any extra spaces) while preserving the <TAB> delimiter using awk gsub but I have not been successful. Original Data: 4365 monte des source rue,, ,<TAB>trevost<TAB>QC Desired Data:... (1 Reply)
Discussion started by: pchang
1 Replies

3. Shell Programming and Scripting

Using multiple gsub() function under a loop in awk

Hi ALL, I want to replace string occurrence in my file "Config" using a external file named "Mapping" using awk. $cat Config ! Configuration file for RAVI ! Configuration file for RACHANA ! Configuration file for BALLU $cat Mapping ravi:ram rachana:shyam ballu:hameed The... (5 Replies)
Discussion started by: useless79
5 Replies

4. Shell Programming and Scripting

Awk, function of underscore char.

Hello Friends, I would appreciate so much if you could explain how the underscores works at the following code? Sorry if it sounds a bit novice question. awk -F',' 'NR==FNR{_=1;next}!_{print}' exclude infile KR, Eagle (6 Replies)
Discussion started by: EAGL€
6 Replies

5. Shell Programming and Scripting

Replace characters in string with awk gsub

Hi I have a source file that looks like a,b,c,d,e,f,g,h,t,DISTI(USD),MSRP(USD),DIST(EUR),MSRP(EUR),EMEA-DISTI(USD),EMEA-MSRP(USD),GLOBAl-DISTI(USD),GLOBAL-MSRP(USD),DISTI(GBP), MSRP(GBP) I want to basically change MSRP(USD) to MSRP,USD and DIST(EUR) to DIST,EUR and likewise for all i'm using... (3 Replies)
Discussion started by: r_t_1601
3 Replies

6. Shell Programming and Scripting

Gsub function in awk

Hello, I had some difficulty to understand the gsub function and maybe the regex in this script to remove all the punctuations: awk 'gsub(//, " ", $0)' text.txtFile text.txt: This is a test for gsub I typed this random text file which contains punctuation like ,.;!'"?/\ etc. The script... (6 Replies)
Discussion started by: yifangt
6 Replies

7. Shell Programming and Scripting

Search & Replace content of files using gsub in awk

Hi,I have 2 files master.txt & reference.txt as shown below & i require o/p as mentioned in file 3 using awk but content is not replacing properlymaster.txt:... (15 Replies)
Discussion started by: siramitsharma
15 Replies

8. Shell Programming and Scripting

Search & replace content using awk/gsub

Hi, I have two files master.txt & reference.txt. Sample below Master.txt 2372,MTS,AP 919848001104,Airtel,DL 0819,MTS,MUM 919849788001,Airtel,AP 1430,Aircel MP,20 Reference.txt 2372,919848701430,46467 919848002372,2372,47195 2372,919849788001,59027 0819,028803,1 0819,029801,1... (2 Replies)
Discussion started by: siramitsharma
2 Replies

9. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

10. Shell Programming and Scripting

awk and gsub - how to replace only the first X occurrences

I have a text (text.txt) and I would like to replace only the first 2 occurrences of a word (but I might need to replace more): For example, if text is this: CAR sweet head hat red yellow CAR book brown tiger CAR cow CAR CAR milk I would like to replace the word "CAR" with word... (12 Replies)
Discussion started by: bingel
12 Replies
Login or Register to Ask a Question