Can I write a string into file from shell?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can I write a string into file from shell?
# 1  
Old 05-09-2007
Can I write a string into file from shell?

Hi ,
If I used this
wc -l *.dat ,it will count number of line in these files like this
1234 test1.dat
2345 test2.dat
3456 test3.dat
7035 total

can I add '|'(pipe) in the middle of those number and string like this
1234|test1.dat
2345|test2.dat
3456|test3.dat

Thanks in advance.
# 2  
Old 05-09-2007
Code:
wc -l *.dat | sed 's/\(^ *[0-9]*\) /\1|/'


Jean-Pierre.
# 3  
Old 05-09-2007
Code:
wc -l *.dat | sed 's/^ *//g;s/  */|/'

# 4  
Old 05-09-2007
Code:
wc -l *.dat | awk 'BEGIN{OFS="|"}{ print $1, $2 }'

# 5  
Old 05-09-2007
awk '{print $1"|"$2}'
# 6  
Old 05-09-2007
Code:
awk '{arr[FILENAME]++}END{for(i in arr) print arr[i]"|"i}' *dat

# 7  
Old 05-09-2007
Or:

Code:
awk 'NR>1&&FNR==1{print n,f}{
	f=FILENAME;n=FNR
}END{
	print n,f;print NR,"Total"
}' OFS="|" *.dat

Use nawk on Solaris.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help to write a shell script to convert text file to excel file.

Hi Everyone, I want your help to write a script which will take text file as input and on the basis of delimiter ":"script will create excel sheet. Example input: IpAdress:InstanceName:Port:ServerName 10.255.255.1:abc:2232:xyz_abc Output should be an excel sheet like below: Column... (8 Replies)
Discussion started by: akabhinav18
8 Replies

2. Shell Programming and Scripting

Need to search a particular String form a file a write to another file using perl script

I have file which contains a huge amount of data. I need to search the pattern Message id. When that pattern is matched I need to get abcdeff0-1g6g-91g3-1z2z-2mm605m90000 to another file. Kindly provide your input. File is like below Jan 11 04:05:10 linux100 |NOTICE... (2 Replies)
Discussion started by: Raysf
2 Replies

3. Shell Programming and Scripting

Regex to split a string and write the output in another file.

hi, i am trying to write a script to generate ouput in the following format: ##### buildappi abcd_sh nodebug.##### ##### buildappi ijk_sh nodebug.##### The given string is as follows: xtopSharedDLLs = "abcd_sh def_sh ijk_sh " \ + "jkl_sh any_sh... (15 Replies)
Discussion started by: Rashid Khan
15 Replies

4. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

5. Shell Programming and Scripting

Write string variables to file

I have the following: #! /bin/bash foo="bar" this="that" vars="foovar=$foo\n\ thisvar=$this\n" I want to write the following to a file: foovar="bar" thisvar="that" Then in another script, I pull this file, and loop through it: while read line; do eval $line done <... (3 Replies)
Discussion started by: Validatorian
3 Replies

6. Shell Programming and Scripting

Extract string from a file & write to a new file (Perl)

Hi, This is the first time playing around with perl and need some help. Assuming if i have a line of text that looks like this: Date/Time=Nov 18 17:12:11;Device Name=192.168.1.1;Device IP=192.168.1.1;Device Class=IDS;Source IP=155.212.212.111;Source Name=UNKNOWN;Source Port=1679... (3 Replies)
Discussion started by: LuckyGuy
3 Replies

7. Shell Programming and Scripting

How to delete a string pattern in a file and write back to the same file

I have a control file which looks like this LOAD DATA INFILE '/array/data/data_Finished_T5_col_change/home/oracle/emp.dat' PRESERVE BLANKS INTO TABLE SCOTT.EMP FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS (................. ..................) How can i edit the... (1 Reply)
Discussion started by: mwrg
1 Replies

8. UNIX for Dummies Questions & Answers

How to write a string with special characters to a file

Hi, I want to erase the contents of a file and write a sql query into the file. "" > tmp.txt "select count\(\*\) \"Names\" from v\$ground where status\=\'Invalid\'\;" > tmp.txt doesnt work, pls help. (2 Replies)
Discussion started by: happyrain
2 Replies

9. UNIX for Dummies Questions & Answers

write string of characters to file

Hi, I have a text file with a very long string of characters, like "ACGCTTGCAA...", and I want to make another file with 36 characters from this string file every 4 lines like: character of position 1 to position 36 of the string file, position 2-37, position 3-38 ... position... (2 Replies)
Discussion started by: fadista
2 Replies

10. Shell Programming and Scripting

line contains a string write to new file otherwise append

hi i have the following function, reading file line by line, if line contains "Change state" i need to cut some fields otherwise i need to append the total line to the new file function parse() { while read LINE do echo $LINE |grep -q "Change state" if then echo $LINE |grep -q... (1 Reply)
Discussion started by: Satyak
1 Replies
Login or Register to Ask a Question