gawk (dos) insert backslash at end of line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting gawk (dos) insert backslash at end of line
# 1  
Old 06-20-2011
gawk (dos) insert backslash at end of line

I have created with the DOS-command dir a list of a directory.

Code:
 Directory of C:\Users\User\Documents

09.06.2011  20:50  48.322 file1.txt
02.11.2010  23:00  9.216 file2.txt
15.12.2010  21:06  26.793 file2.txt

Now i would like to add the directory ahead of the filename. Therefore a backslash needs to be added at the end of $0 or in my case the var p. This does not work, though when I add a word it works.

Output with a string:
Code:
gawk" "/ Directory of / {p=substr($0,14)} {if (NF>3) {print p " test " $4}

Output:

Code:
C:\Users\User\Documents test file1.txt 48.322
 C:\Users\User\Documents test file2.txt  9.216
 C:\Users\User\Documents test file1.txt 26.793

Can anybody help?

Code:
gawk" "/ Directory of / {p=substr($0,14)} {if (NF>3) {print p \"\\\" $4}

All these varietys return an empty file:
{print p \"\\\" $4} = empty file
{print p "\\" $4} = empty fileFB_Addon_TelNo{ height:15px !important; white-space: nowrap !important; background-color: #0ff0ff;}

Last edited by sdf; 06-20-2011 at 08:53 AM..
# 2  
Old 06-20-2011
Hi

Not sure if this is what you wanted. If not, please post the actual output you expected:


Code:
$ awk '/ Directory of /{p=substr($0,14)} {if (NF>3){print p""$4;}}' a
 C:\Users\User\Documents\file1.txt
 C:\Users\User\Documents\file2.txt
 C:\Users\User\Documents\file2.txt

Guru.
# 3  
Old 06-20-2011
In what environment do you run gawk? In DOS?
# 4  
Old 06-20-2011
Sorry, made a mistake. The directory looks like this without backslash at the end.
Code:
Directory of C:\Users\User\Documents  
09.06.2011  20:50  48.322 file1.txt 
02.11.2010  23:00  9.216 file2.txt 
15.12.2010  21:06  26.793 file2.txt

Desired output with backslash between the var p and filename
C:\Users\User\Documents\file1.txt
C:\Users\User\Documents\file2.txt
C:\Users\User\Documents\file2.txt

---------- Post updated at 06:05 PM ---------- Previous update was at 01:58 PM ----------

Quote:
Originally Posted by yazu
In what environment do you run gawk? In DOS?
Yes the operating system is Windows 7 on Command line with dos. Since i can't get the backslash to be passed. How about getting this backslash with getline < backslash.txt containing only the backslash. Can somebody help on the code for file input in an one liner?

Last edited by sdf; 06-20-2011 at 09:08 AM..
# 5  
Old 06-20-2011
How about:

Code:
var=sprintf("%c", 0134);

where 0134 is the octal value of a backslash. Then you can use that var wherever you like. print var "stuff" prints \stuff
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 06-20-2011
This should work:
Code:
gawk "/Directory of/{p=$NF; next} NF{printf(\"%s%c%s\n\", p, 0x5C, $NF)}" file

This User Gave Thanks to Franklin52 For This Post:
# 7  
Old 06-21-2011
Quote:
Originally Posted by Corona688
How about:

Code:
var=sprintf("%c", 0134);

where 0134 is the octal value of a backslash. Then you can use that var wherever you like. print var "stuff" prints \stuff
With this sample
Code:
var=sprintf("%c", 0134);

I got the idea to assign
Code:
-v bs=\

Even this works as a FS for the split() command
Code:
-v bs=[\._]

This finally worked best with DOS
Code:
 
gawk -v bs=\ "/ Directory of / {p=substr($0,14)} {if (NF>3) {print p bs $4}"

This User Gave Thanks to sdf 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

Gawk --- produce the output in pattern space instead of END space

hi, I'm trying to calculate IP addresses and their respective calls to our apache Server. The standard format of the input is HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST... (2 Replies)
Discussion started by: busyboy
2 Replies

2. UNIX for Dummies Questions & Answers

insert string at end of line if it found from list

Hi all, can some one help me out file 1 i have 06/01 3:14 d378299 06/01 8:10 d642036 06/01 10:51 d600441 06/01 10:52 d600441 06/01 11:11 d607339 06/01 11:49 d398706 something like this and in file named list i have ( there is space btwn 06/01 and 11:49 and d398706) d607339... (5 Replies)
Discussion started by: zozoo
5 Replies

3. UNIX for Dummies Questions & Answers

Replace backslash at the end of the string using sed command

I have text file which is a tab delimited one. Sample data from the file is shown below: unix is\ great\ os linux\ is superb I want to replace that backslash with empty string preserving the tab delimiter. Output should be unix is great os linux is ... (3 Replies)
Discussion started by: p.akhilreddy4u
3 Replies

4. Shell Programming and Scripting

Redirect output directly with DOS end of line

Hi guys, I have some echo scripts and awk scripts like these: echo "some text" > output1 . . awk '{....}{print}' input > output2Both output1 and output2 are saved with unix END Of Line, then, is there an option to include within echo command and awk command to save the output as DOS END Of... (3 Replies)
Discussion started by: Ophiuchus
3 Replies

5. Shell Programming and Scripting

How to disable backslash in command line

Dear I have one problem in SCO Unix command line. I want to disable execution of commands with backslash. For example - if the user executes the following command \exec_script Here - i want the system should NOT execute this command. Can any one give me a suitable... (8 Replies)
Discussion started by: subbu62
8 Replies

6. Shell Programming and Scripting

How to retain backslash in a line while reading a data file?

Hello Firends I have a file that contains data within single quotes, which has meaning of its own. When I am trying to parse through the file for a different functionality I noticed that I was loosing the backslash when occurrences in the file look like ('\0'). I would want to retain the... (3 Replies)
Discussion started by: easwam
3 Replies

7. Shell Programming and Scripting

insert text at every end of the line

Hi, I just wanted to know if you have any idea or script to insert a text at everyend of the line, the text will vary. for example sample: this is line1 ok this is line2 ok this is line3 ok output: this is line1 ok /home/line1.txt this is line2 ok /home/line2.txt this is line3 ok... (6 Replies)
Discussion started by: invinzin21
6 Replies

8. UNIX for Dummies Questions & Answers

insert period at the end of each line

file1 contains: this is a test this is a test and only a test this is another test this is another test and only another only i'd like my file to look like this: this is a test. this is a test and only a test. this is another test. this is another test and only another only. (6 Replies)
Discussion started by: tjmannonline
6 Replies

9. Shell Programming and Scripting

Insert two strings at the beginning and at the end of each line of a file

Hi, excuse me for my poor english. My problem is that: I have a File i want to add to each line of that file two strings: one at the beginning of the line, one at the ending. string1="abcd" string2="efgh" i want $string1 content $string2 for each line. Is that possible? (3 Replies)
Discussion started by: Linux-fueled
3 Replies

10. UNIX for Dummies Questions & Answers

How can I insert character to end of file?

Hello all How can I insert character to the end of text file without opening it in vi Just simple one liner, can it be done? Tnx (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question