Remove the comma on at the end of every 4th line [closed] - linux

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Her is a example of the output. I need to remove the comma at the end of every fourth line.
"tester1",
"SERVICE_TICKET_CREATED",
"Thu Mar 19 23:27:57 UTC 2015",
"111.111.11.1"
"tester1",
"SERVICE_TICKET_CREATED",
"Fri Mar 20 00:31:59 UTC 2015",
"111.111.11.1"
What I am trying to do is
"tester1",
"SERVICE_TICKET_CREATED",
"Thu Mar 19 23:27:57 UTC 2015",
"111.111.11.1"
"tester1",
"SERVICE_TICKET_CREATED",
"Fri Mar 20 00:31:59 UTC 2015",
"111.111.11.1"

With GNU sed:
sed '4~4 s/,$//' filename
I have to point out, though, that in your example output there's no comma at the end of every fourth line.
This should work for you, since the question is tagged "linux" and Linux very nearly always comes with GNU sed. For the sake of completeness: with BSD sed (as found on Mac OS X and *BSD), the 4~4 pattern does not work (it is a GNU extension). There you could do something like
sed 'n;n;n;s/,$//' filename
...which fetches and prints three extra lines every time and removes the comma at the end of the fourth (unless the end of the input was reached before a fourth line could be fetched).
Alternatively, with awk you could use
awk 'NR % 4 == 0 { sub(/,$/, "") } 1' filename

Related

Shell script to convert a date string to specific timestamp format

I have a date time string like this 17-Mar-2020.22:22 -0300.I want to get the output as 2020-03-17 22:12 .
The timezone offset is causing an issue as without that I am able to convert
For example DATE=17-Mar-2020.22:22
date -jf "%d-%b-%Y.%H:%M" $DATE "+date \"%Y-%m-%d %H:%M\""
But this timezone offset is causing an issue.Can someone please help?
Because the input is in an unusual format, it needs to be first formatted to a format date can eat. I use the format indicated in man page "2004-02-29 16:21:42" or "Sun, 29 Feb 2004 16:21:42 -0800". GNU date allows for very flexible input format - don't trust it.
Below I used sed to match the input and reformat it.
input='17-Mar-2020.22:22 -0300'
input2="$(sed 's/\([0-9]*\)-\([^-]*\)-\([^\.]*\)\.\([0-9]*\):\([0-9]*\)/\1 \2 \3 \4:\5:00/' <<<"$input")"
# input2="17 Mar 2020 22:22:00 -0300"
date --date="$input2" +"%Y-%m-%d %H:%M"
# 2020-03-18 02:22
date -jf
GNU date differs a lot from BSD date. They are completely different. BSD date supports the -j and -f options, GNU date does not.
There are dateutils and they include strptime utility that allows to do what you wanted to do with date:
strptime -e -f "%Y-%m-%d %H:%M\n" -i "%d-%b-%Y.%H:%M %Z" "17-Mar-2020.22:22 -0300"

Break log files after first occurrence of a string in unix [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
On a Unix server the log file has appended since long and now it size is 42 gb.
I have to check all logs for after the first occurrence of a particular date i.e: Nov 12 , 2018.
I need all logs for the date Nov 12, 2018. What is the best possible way to do it?
Assuming you are looking for Nov 12 , 2018 text in the log file you can use sed to print everything after Nov 12 , 2018 is matched:
sed -ne '/Nov 12 , 2018/,$ p' <path to log file>
If the date is always at the beginning of the line you can use grep with regex to filter out specific lines:
grep -e "^Nov 12 , 2018" <path to log file>
If you only need the logs for that specific date just do:
grep "Nov 12, 2018" file.log
you can do:
grep -e "$Nov 12 , 2018" yourlogfile.txt > filteredlog.txt
make sure you match the date and year correctly.

Printing two variables - only second one printed when run from script

I'm trying to print to sting1 and string2 with a space in between using shell script.
defined strings a,b and printing them using echo with a space in between. However, a is replaced by b as below.
a='30 Jan 2016 22:'
b='30 Jan 2016 23:'
echo $a $b
Output:
30 Jan 2016 23:
String1 is override by string2 to display. However, the same is working from command prompt as below:
$ a='30 Jan 2016 22:'
$ b='30 Jan 2016 23:'
$ echo $a $b
30 Jan 2016 22: 30 Jan 2016 23:
Why do these behaviors differ?
The most likely cause of this is DOS newlines in your text files. This means you have a CRLF at the end of each line, sending the cursor back to the beginning.
When interpreted by UNIX tools, for which a LF (\n) is the only character involved in a newline, the CR (\r) preceding it becomes part of the data.
That makes the actual behavior of your code this:
a='30 Jan 2016 22:'$'\r'
b='30 Jan 2016 23:'$'\r'
echo $a $b
...first printing $a, then returning the cursor to the beginning of the line and overwriting it with $b.

Want to assign the result of the command echo ^`date` to a variable. but when i am trying to do i am not getting the excepted result

echo ^`date`
^Wed Jan 21 05:49:37 CST 2015
deeps=`echo ^`date``
echo $deeps
Actual Result:
^date
Expected Result :
^Wed Jan 21 05:49:37 CST 2015
Need help on this
Try this method
deeps=^$(date)
echo $deeps
Output :
^Wed Jan 21 18:44:25 IST 2015
Backticks are horribly outdated and should not be used any more -- using $() instead will save you many headaches
Use a backtick, or use Command substitution. Like
# Shell command substitution.
echo ^$(date)
or
# backticks.
deeps=`date`
echo ^$deeps
Both output (the requested)
^Wed Jan 21 08:16:01 EST 2015
Simply because neither of the other (correct) answers actually explain the problem I'm adding another answer.
tl;dr Compare the backtick version to the $() version
The difference between
echo ^`date`
and
deeps=`echo ^`date``
is how many backticks are on the line and how the shell is parsing the line.
echo ^`date`
has a single pair of backticks and the shell parses it as (where the [] are marking "parts" of the line)
[echo] [^][`date`]
The
`date`
bit is then expanded via Command Substitution and so the line becomes
[echo] [^Wed Jan 21 05:49:37 CST 2015]
and then echo spits out the desired ^Wed Jan 21 05:49:37 CST 2015.
This line however
deeps=`echo ^`date``
is parsed as
[deeps][=][`echo ^`][date][``]
which you can already see is quite different and not correct (this happens because backticks cannot be nested for the record).
There are now two command substitutions on this line echo ^ and the empty string so the line becomes
[deeps][=][^][date][]
or with the "words" combined
[deeps][=][^date]
which then assigns ^date to deeps and echo $deeps then gets you ^date.
The $() form of command substitution, no the other hand, does nest and thus
deeps=$(echo ^$(date))
parses as
[deeps][=][$([echo] [^][$([date])])]
which properly runs both date and echo on the result. Though, as indicated in the other answers, the wrapping echo is not necessary as deeps=^$(date) will work just fine by itself.

BASH date command: can't re-combine date and time

Linux v2.4/Bash v3.2/GNU utils/date command version 5.0
I'm struggling with the date command. In a BASH application, the user can set date and time separately, resulting in separate variables for date and time. Further on, these variables are re-combined but this appears not be palatable for the date command: I get a different date back. Time is the same, however. Testing code:
#!/bin/bash
dnow1="$(date)"
echo "1 $dnow1" # --> Sat Sep 14 16:31:48 EDT 2013
#split date and time
dldate="$(date -d "$dnow1" +"%d-%m-%Y")"
echo "2 $dldate" # --> 14-09-2013
dltime="$(date -d "$dnow1" +"%H:%M:%S")"
echo "3 $dltime" # --> 16:31:48
#try to re-combine date and time
string="${dldate} ${dltime}"
echo "4 $string" # --> 14-09-2013 16:31:48
dnow2="$(date -d "$string")"
echo "5 $dnow2" # --> Thu Mar 5 16:31:48 EST 2020
I must be missing something here. Can anyone enlighten me? Thanks!
Note:
I'm working an original XBOX that has few/low resources so there's no room for other solutions like Python. I'm a 'bashist' anyway so it must be BASH!
Edit: corrected time format. Thanks Mat.
As to "$(....)" I have made it a habit to double quote wherever possible.
When getting your date use this format instead:
#split date and time
dldate="$(date -d "$dnow1" +"%Y-%m-%d")"
From GNU date manual
The output of the date command is not always acceptable as a date string, not only because of the language problem, but also because there is no standard meaning for time zone items like ‘IST’. When using date to generate a date string intended to be parsed later, specify a date format that is independent of language and that does not use time zone items other than ‘UTC’ and ‘Z’.
First of all using -d won't work in 14-09-2013 fashion, you can easily set date and time with one command and put it into variable. eg, just try this below on shell and then you can put into shell script.
date --date="Feb 2 2014 13:12:10"
Sun Feb 2 13:12:10 PST 2014

Resources