Question on awk source files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question on awk source files
# 1  
Old 08-16-2016
Question on awk source files

Im repeating same command to get count, filename from 4 different files, writing to one same file.
Code:
awk 'END{print NR"|"FILENAME}' file.txt >> temp.txt; 
awk 'END{print NR"|"FILENAME}' asdf.txt >> temp.txt; 
awk 'END{print NR"|"FILENAME}' lkjh.txt >> temp.txt;
awk 'END{print NR"|"FILENAME}' bvcx.txt >> temp.txt

Anyway above commands can be clubbed into one single line so no need to repeat my awk command multiple times ???
# 2  
Old 08-16-2016
Linux

If you use GNU AWK, the following command will work well.

Code:
gawk 'ENDFILE{print NR"|"FILENAME}' file.txt asdf.txt lkjh.txt bvcx.txt

# 3  
Old 08-16-2016
looks like
sh: gawk: not found.
# 4  
Old 08-16-2016
Quote:
Originally Posted by JSKOBS
looks like
sh: gawk: not found.
Hello JSKOBS,

With simple awkalso it should work, could you please try following.
Code:
awk '{print NR"|"FILENAME}' file.txt  asdf.txt  lkjh.txt  bvcx.txt   > temp.txt

NOTE: If you need line numbers from 1 to till the total of lines(counting all Input_files) then NR as used above is fine, if you want to RESET count for every Input_file then in above you should use FNR in place of NR.

Thanks,
R. Singh
# 5  
Old 08-16-2016
Hi Ravinder, i think i confused...
we need the file record count like below, not each line count
Code:
2342343|file.txt
762834623|asdf.txt  
23|lkjh.txt  
9098|bvcx.txt


Moderator's Comments:
Mod Comment Please use CODE (not PHP) tags as required by forum rules!

Last edited by RudiC; 08-16-2016 at 05:48 AM.. Reason: Added CODE tags.
# 6  
Old 08-16-2016
Try:
Code:
awk 'FNR==1&&p{print p};{p=FNR"|"FILENAME};END{print FNR"|" FILENAME}' file.txt  asdf.txt  lkjh.txt  bvcx.txt >> temp.txt

# 7  
Old 08-16-2016
Quote:
Originally Posted by JSKOBS
Hi Ravinder, i think i confused...
we need the file record count like below, not each line count
PHP Code:
2342343|file.txt
762834623
|asdf.txt  
23
|lkjh.txt  
9098
|bvcx.txt 
Hello JSKOBS,

If you want simple and sober line counts the you could use wc command as follows.
Code:
wc -l Input_file1  Input_file2  Input_file3  Input_file4  | grep -v "total"

Output will be as follows.
Code:
  12 Input_file1
   4 Input_file2
   4 Input_file3
   5 Input_file4

If you want your output to be delimited with | and no space in starting as shown in above output then you could do following.
Code:
wc -l Input_file1  Input_file2  Input_file3  Input_file4  | awk '($0 ~ /total/){next}{sub(/^[[:space:]]+/,X,$0);sub(/[[:space:]]/,"|",$0);} 1'

Output will be as follows.
Code:
12|Input_file1
4|Input_file2
4|Input_file3
5|Input_file4

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error files count while coping files from source to destination locaton as well count success full

hi All, Any one answer my requirement. I have source location src_dir="/home/oracle/arun/IRMS-CM" My Target location dest_dir="/home/oracle/arun/LiveLink/IRMS-CM/$dc/$pc/$ct" my source text files check with below example.text file content $fn "\t" $dc "\t" $pc "\t" ... (3 Replies)
Discussion started by: sravanreddy
3 Replies

2. UNIX for Dummies Questions & Answers

Concatenate files and delete source files. Also have to add a comment.

- Concatenate files and delete source files. Also have to add a comment. - I need to concatenate 3 files which have the same characters in the beginning and have to remove those files and add a comment and the end. Example: cat REJ_FILE_ABC.txt REJ_FILE_XYZ.txt REJ_FILE_PQR.txt >... (0 Replies)
Discussion started by: eskay
0 Replies

3. Shell Programming and Scripting

Awk command with two source files

Hello, I have two source files: sourcefile1.dat: 12345 xxx yyy zzz 23456 qqq ttt rrr 34567 ppp jjj ggg 45678 fff ddd sss 56789 nnn mmm ccc sourcefile2.dat: 12345.gif 34567.gif I want to obtain a simple awk one linger to obtain the following: xxx yyy zzz 12345.gif qqq ttt rrr... (15 Replies)
Discussion started by: palex
15 Replies

4. Shell Programming and Scripting

need a shell script to extract the files from source file and check whether those files existonserve

Hi, I am new to shell scripting.Please help me on this.I am using solaris 10 OS and shell i am using is # echo $0 -sh My requirement is i have source file say makefile.I need to extract files with extensions (.c |.cxx |.h |.hxx |.sc) from the makefile.after doing so i need to check whether... (13 Replies)
Discussion started by: muraliinfy04
13 Replies

5. Solaris

Question about SunOS version, how to include in C source

Sorry if here is the wrong place to put this question, but for college we develop small programs in C using Solaris. Most of time is OK for us not to document nothing, until now. Every time program is executed must print OS name. Does Solaris has some predefined macros which I can include... (3 Replies)
Discussion started by: solaris_user
3 Replies

6. UNIX for Dummies Questions & Answers

Source all files in a directory

Hi everyone, Is there an efficient way to source all of the files contained in a directory? Theoretically I could create a FOR loop and successively source each file, but I just wanted to check if there was a cleaner method. Thanks! Mike (3 Replies)
Discussion started by: msb65
3 Replies

7. Shell Programming and Scripting

Awk help with source and previous line loop

Hello, I've written a ksh awk script to ping multiple servers and write the results to a file. That part is working ok. I then want to extract the names of only the server which are available. This is indicated by '1 packets received'. The server name actually appears above that line so I found... (4 Replies)
Discussion started by: Grueben
4 Replies

8. Programming

Vi question for writing source code

Hi guys, I'm modifying an old f77 code using vi. I'm modifying the code remotely from my windows machine using xming using vi. I'm using tabs to move past the first 6 columns of the code and to keep my loops and if statements neat, but when I hit the tab key, vi displays a big red block which is... (7 Replies)
Discussion started by: rks171
7 Replies

9. Shell Programming and Scripting

noob question - is awk the tool to clean dirty text files?

Hi, nevermind. I think I've found the answer. It appears I was looking for index, match, sub, and gsub. I want to write a shell script that will clean the html out of a bunch of files and format the data for import into excel. Awk seems like a powerful tool, but it seems oriented to... (1 Reply)
Discussion started by: yogert909
1 Replies

10. OS X (Apple)

open-source driver question

Hi, I'm a linux guy and have used netbsd, openbsd, freebsd etc in the past but never tangled with the kernel or drivers outside of Linux. My mother has fried her ethernet port on her iMac (G4 I think); I recently sent her a silicom USB U2E (usb 2 ethernet) dongle which is evidently not... (2 Replies)
Discussion started by: sjalex
2 Replies
Login or Register to Ask a Question