Cygwin shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cygwin shell script
# 1  
Old 05-21-2013
Cygwin shell script

I have a file with below format :

Code:
2013-05-20  hello
has done
2013-05-20  hi
000abc
abc
2013-05-21 thank you


I want the out put to be like this :

Code:
2013-05-20  hello has done
2013-05-20  hi 000abc abc
2013-05-21 thank you


What I could think of is something like this :

Code:
cat input.txt | sed -r ':a ;$! N; s/\n([0-9]{4}-[0-9]{2}-[0-9]{2})/ \1/; ta ; P ; D' >> result.txt;

Problem with the above script is it does this :

Code:
2013-05-20  hello
has done 2013-05-20  hi 
000abc
abc 2013-05-21] thank you


That means I need to put a not(!) in the date format
Code:
(([0-9]{4}-[0-9]{2}-[0-9]{2})

. Kindly help!

Thanks in advance

Last edited by Scrutinizer; 05-22-2013 at 03:32 AM.. Reason: code tags also for sample data
# 2  
Old 05-21-2013
If you assume all line with 2013 are years. (you can modify regex to fit your need)
Code:
awk '/^2013/ {print s;s=$0} !/^2013/ {s=s" "$0} END {print s}'

2013-05-20 hello has done
2013-05-20 hi 000abc abc
2013-05-21 thank you

# 3  
Old 05-21-2013
Really thanks for the reply!

Here in my example we are assuming the date to be 2013 but it can be anything. So it would be better if we consider the complete date 2013-05-20 as pattern.
Also as I am new to shell scripting can you please let me know how can I output the result in a file?
# 4  
Old 05-21-2013
Code:
awk '/^[1-2][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]/ {print s;s=$0} !/^[1-2][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]/ {s=s" "$0} END {print s}'

Less precise version
Code:
awk --posix '/^[0-9-]{10}/ {print s;s=$0} !/^[0-9-]{10}/ {s=s" "$0} END {print s}' file

This will test if first 10 characters are 0 to 9 or -
You need this --posix,-W posix or this --re-interval option to make {x} work

Last edited by Jotne; 05-21-2013 at 08:11 AM..
# 5  
Old 05-21-2013
Thanks a lot Smilie
# 6  
Old 05-22-2013
Slight change to simplify and remove blank fist line:

Code:
awk '/^[1-2][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]/{if(s)print s;s=$0;next} {s=s" "$0} END {print s}'

This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 05-22-2013
Thanks for the reply. I had already put one sed command to remove all empty line, but this code to remove the first line looks great.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execute a shell script in UNIX server from Cygwin

Hi All, We have shell scripts in UNIX ( Sun OS ) server. Also in my windows machine i have Cygwin installed. Now is there a way to execute Shell script available in UNIX server from Cygwin? (4 Replies)
Discussion started by: Girish19
4 Replies

2. Shell Programming and Scripting

Running q-shell commands( on IBM-i Series) from cygwin terminal (on windows)

I have cygwin installed on windows server and when I do echo $SHELL the output is /bin/bash I have created a ssh tunnel from this windows server through cygwin to ibm -i series which is running Q-shell. I am trying to invoke a utility wsadmin (used for scripting) on ibm-i from the... (12 Replies)
Discussion started by: gaurav99
12 Replies

3. Shell Programming and Scripting

cygwin executable shell scripts

Hello, Cygwin will execute a shell script without turning on executable status on the file. I want to force cygwin to be more like linux and not execute scripts directly on the command line unless changing mode to have executable status. Is this possible? Thanks Larry (2 Replies)
Discussion started by: larrye0123
2 Replies

4. Shell Programming and Scripting

Running shell script in Cygwin terminal

I am new to shell scripting. I tried to run a simple shell script using Cygwin terminal in Win XP env. The script I have written is as follows - #!/bin/bash a=5 ] && echo "true" || echo "false" But when I execute the script, getting some confusing error. The error I am getting are - ... (3 Replies)
Discussion started by: linux_learner
3 Replies

5. Shell Programming and Scripting

opening new instance of cygwin from withing cygwin

I'm using cygwin on win7, What I would like to do is something like this: cygstart cygwin tail -f /foo/test.log | perl -pe 's/error/\e I know I can start a new instance using either of these: mintty -e ... cygstart tail ... But neither of those open in ANSI mode, so I can't do... (0 Replies)
Discussion started by: Validatorian
0 Replies

6. Shell Programming and Scripting

Cygwin-shell script

hi, I am using Cygwin for running the shell script. If i execute the following command at the prompt output is obtained. grep echo * The same i have put in a file and when am trying to execute it is throwing error. IP.sh echo "Starting command..........." grep echo * ... (9 Replies)
Discussion started by: flamingo_l
9 Replies

7. Shell Programming and Scripting

cygwin shell script error

Hi, I have a cywin test script as below but errors, #!/usr/bin/bash while do echo "123" echo "456" done sh-3.2$ ./test.sh ./test.sh: line 7: syntax error: unexpected end of file Please help... Thanks! Victor (3 Replies)
Discussion started by: victorcheung
3 Replies

8. Shell Programming and Scripting

Execute a shell with cygwin

Hi everybody, I would like to execute a shell in windows. So I installed Cygwin but I got this message : $ sh /neuf.sh start % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 ... (4 Replies)
Discussion started by: mytrax
4 Replies

9. UNIX for Dummies Questions & Answers

Cygwin shell not coming up

Hi, I installed Cygwin y'day on Win XP SP2. The install went fine but when I clicked on Cygwin's shortcut to invoke its shell, it didn't come up.....was just opening for a fraction of second and then closed. Can anyone please advise? Regards er_ashu (6 Replies)
Discussion started by: er_ashu
6 Replies

10. UNIX for Dummies Questions & Answers

Problem with scheduling a shell script on cygwin using cron

Hi, Before I start, I would like to inform that, I went through all FAQs and other threads before posting here. I 'm trying to schedule a shell script on cygwin using cron. No matter what I do I don't seem to get the cron job executing my bash script. My script is temp.sh echo `date` >... (4 Replies)
Discussion started by: shash
4 Replies
Login or Register to Ask a Question