How can we pass month value as per our requirements?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can we pass month value as per our requirements?
# 1  
Old 10-27-2015
How can we pass month value as per our requirements?

how to display the record according as per my requirement if i want to display the record may-v to apr-v and may-m to apr-m with all the records we can pass month as per our requirements
Table-----

Code:
NO|NAME|JAN-V|FEB-V|MAR-V|APR-V|MAY-V|JUN-V|JUL-V|AUG-V|SEP-V|OCT-V|NOV-V|DEC-V|JAN-M|FEB-M|MAR-M|APR-M|MAY-M|JUN-M|JUL-M|AUG-M|SEP-M|OCT-M|NOV-M|DEC-M
10|AAAA|1001|1002|1003|1004|1005|1006|1007|1008|1009|1010|1011|1012|2001|2002|2003|2004|2005|2006|2007|2008|2009|2010|2011|2012
20|BBBB|1111|1112|1113|1114|1115|1116|1117|1118|1119|1110|1111|1112|2221|2222|2223|2224|2225|2226|2227|2228|2229|2210|2211|2212

Output should be like this
Code:
NO|NAME|MAY-V|JUN-V|JUL-V|AUG-V|SEP-V|OCT-V|NOV-V|DEC-V|JAN-V|FEB-V|MAR-V|APR-V|MAY-M|JUN-M|JUL-M|AUG-M|SEP-M|OCT-M|NOV-M|DEC-M|JAN-M|FEB-M|MAR-M|APR-M
10|AAAA|1005|1006|1007|1008|1009|1010|1011|1012|1001|1002|1003|1004|2005|2006|2007|2008|2009|2010|2011|2012|2001|2002|2003|2004
20|BBBB|1115|1116|1117|1118|1119|1110|1111|1112|1111|1112|1113|1114|2225|2226|2227|2228|2229|2210|2211|2212|2221|2222|2223|2224


Last edited by jim mcnamara; 10-27-2015 at 01:34 PM.. Reason: add code tags correctly
# 2  
Old 10-27-2015
Okay. What have you tried so far?
# 3  
Old 10-27-2015
I tried but it should be display as per requirements when I can pass month values the record should be display above mentioned output it should be apr to mar or jan to dec
Code:
awk -F "|" {print $1,$2,$7,$8,$9,$10,$11,$12,$14,$3,$4,$5,$6,$2,$19,$20,$21,$22,$23,$24,$25,$26,$14,$15,$16,$18}


Last edited by Don Cragun; 10-27-2015 at 03:34 PM.. Reason: Change ICODE tags to CODE tags.
# 4  
Old 10-27-2015
Try
Code:
awk -F\| '
        {printf "%s|%s", $1, $2
         for (i=STARTM; i<=12; i++) printf "|%s", $(i+2)
         for (i=1; i<STARTM; i++) printf "|%s", $(i+2)
         for (i=STARTM; i<=12; i++) printf "|%s", $(i+14)
         for (i=1; i<STARTM; i++) printf "|%s", $(i+14)
         printf "\n"
        }
' STARTM=5 file
NO|NAME|MAY-V|JUN-V|JUL-V|AUG-V|SEP-V|OCT-V|NOV-V|DEC-V|JAN-V|FEB-V|MAR-V|APR-V|MAY-M|JUN-M|JUL-M|AUG-M|SEP-M|OCT-M|NOV-M|DEC-M|JAN-M|FEB-M|MAR-M|APR-M
10|AAAA|1005|1006|1007|1008|1009|1010|1011|1012|1001|1002|1003|1004|2005|2006|2007|2008|2009|2010|2011|2012|2001|2002|2003|2004
20|BBBB|1115|1116|1117|1118|1119|1110|1111|1112|1111|1112|1113|1114|2225|2226|2227|2228|2229|2210|2211|2212|2221|2222|2223|2224

---------- Post updated at 20:07 ---------- Previous update was at 19:47 ----------

OR:
Code:
awk -F\| '
        {printf "%s|%s", $1, $2
         for (i=1; i<=24; i++) printf "|%s", $(3 + (STARTM-2+i)%12 + 12*int((i-1)/12))
         printf "\n"
        }
' STARTM=8 file
NO|NAME|AUG-V|SEP-V|OCT-V|NOV-V|DEC-V|JAN-V|FEB-V|MAR-V|APR-V|MAY-V|JUN-V|JUL-V|AUG-M|SEP-M|OCT-M|NOV-M|DEC-M|JAN-M|FEB-M|MAR-M|APR-M|MAY-M|JUN-M|JUL-M
10|AAAA|1008|1009|1010|1011|1012|1001|1002|1003|1004|1005|1006|1007|2008|2009|2010|2011|2012|2001|2002|2003|2004|2005|2006|2007
20|BBBB|1118|1119|1110|1111|1112|1111|1112|1113|1114|1115|1116|1117|2228|2229|2210|2211|2212|2221|2222|2223|2224|2225|2226|2227

# 5  
Old 10-27-2015
If the input file doesn't always start with January as the first month in the table, you could also try something like:
Code:
#!/bin/ksh
IAm=${0##*/}
if [ $# -lt 1 ] || [ $# -gt 2 ]
then	printf '%s: Wrong number of arguments.\n' "$IAm" >&2
	printf 'Usage: %s first_month_abbreviation [ pathname ]\n' "$IAm" >&2
	exit 1
fi
M1="$1"
File="${2:-file}"
if [ ! -r "$File" ]
then	printf "%s: Can't read file: %s\n" "$IAm" "$File"
	exit 2
fi
awk -v M1="$M1" '
BEGIN {	M1 = toupper(M1)
	FS = OFS = "|"
}
NR == 1 {
	for(i = 3; i <= 14; i++)
		if(M1 == toupper(substr($i, 1, 3))) {
			f1 = i - 3
			break
		}
	if(i == 15)
		exit 10
}
{	y1 = y2 = ""
	for(i = f1; i < f1 + 12; i++) {
		y1 = y1 OFS $(3 + (i % 12))
		y2 = y2 OFS $(15 + (i % 12))
	}
	print $1, $2 y1 y2
}' "$File" && exit
printf '%s: first_month_abbreviation (%s) not found.\n' "$IAm" "$M1" >&2
printf '%s: Need 3 character case-insensitive abbreviateion (e.g., Jan)\n' \
    "$IAm" >&2
printf 'Usage: %s first_month_abbreviation [ pathname ]\n' "$IAm" >&2
exit 3

If your sample data is in a file named file and you named this script monthrotate, the command:
Code:
./monthrotate may > out

would place the output:
Code:
NO|NAME|MAY-V|JUN-V|JUL-V|AUG-V|SEP-V|OCT-V|NOV-V|DEC-V|JAN-V|FEB-V|MAR-V|APR-V|MAY-M|JUN-M|JUL-M|AUG-M|SEP-M|OCT-M|NOV-M|DEC-M|JAN-M|FEB-M|MAR-M|APR-M
10|AAAA|1005|1006|1007|1008|1009|1010|1011|1012|1001|1002|1003|1004|2005|2006|2007|2008|2009|2010|2011|2012|2001|2002|2003|2004
20|BBBB|1115|1116|1117|1118|1119|1110|1111|1112|1111|1112|1113|1114|2225|2226|2227|2228|2229|2210|2211|2212|2221|2222|2223|2224

in the file named out. And, then the command:
Code:
./monthrotate Jan out

would restore the output produced by the first command back to its original order:
Code:
NO|NAME|JAN-V|FEB-V|MAR-V|APR-V|MAY-V|JUN-V|JUL-V|AUG-V|SEP-V|OCT-V|NOV-V|DEC-V|JAN-M|FEB-M|MAR-M|APR-M|MAY-M|JUN-M|JUL-M|AUG-M|SEP-M|OCT-M|NOV-M|DEC-M
10|AAAA|1001|1002|1003|1004|1005|1006|1007|1008|1009|1010|1011|1012|2001|2002|2003|2004|2005|2006|2007|2008|2009|2010|2011|2012
20|BBBB|1111|1112|1113|1114|1115|1116|1117|1118|1119|1110|1111|1112|2221|2222|2223|2224|2225|2226|2227|2228|2229|2210|2211|2212

As always, if someone wants to try this script on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
# 6  
Old 10-27-2015
Quote:
Originally Posted by RudiC
Try
Code:
awk -F\| '
        {printf "%s|%s", $1, $2
         for (i=STARTM; i<=12; i++) printf "|%s", $(i+2)
         for (i=1; i<STARTM; i++) printf "|%s", $(i+2)
         for (i=STARTM; i<=12; i++) printf "|%s", $(i+14)
         for (i=1; i<STARTM; i++) printf "|%s", $(i+14)
         printf "\n"
        }
' STARTM=5 file
NO|NAME|MAY-V|JUN-V|JUL-V|AUG-V|SEP-V|OCT-V|NOV-V|DEC-V|JAN-V|FEB-V|MAR-V|APR-V|MAY-M|JUN-M|JUL-M|AUG-M|SEP-M|OCT-M|NOV-M|DEC-M|JAN-M|FEB-M|MAR-M|APR-M
10|AAAA|1005|1006|1007|1008|1009|1010|1011|1012|1001|1002|1003|1004|2005|2006|2007|2008|2009|2010|2011|2012|2001|2002|2003|2004
20|BBBB|1115|1116|1117|1118|1119|1110|1111|1112|1111|1112|1113|1114|2225|2226|2227|2228|2229|2210|2211|2212|2221|2222|2223|2224

---------- Post updated at 20:07 ---------- Previous update was at 19:47 ----------

OR:
Code:
awk -F\| '
        {printf "%s|%s", $1, $2
         for (i=1; i<=24; i++) printf "|%s", $(3 + (STARTM-2+i)%12 + 12*int((i-1)/12))
         printf "\n"
        }
' STARTM=8 file
NO|NAME|AUG-V|SEP-V|OCT-V|NOV-V|DEC-V|JAN-V|FEB-V|MAR-V|APR-V|MAY-V|JUN-V|JUL-V|AUG-M|SEP-M|OCT-M|NOV-M|DEC-M|JAN-M|FEB-M|MAR-M|APR-M|MAY-M|JUN-M|JUL-M
10|AAAA|1008|1009|1010|1011|1012|1001|1002|1003|1004|1005|1006|1007|2008|2009|2010|2011|2012|2001|2002|2003|2004|2005|2006|2007
20|BBBB|1118|1119|1110|1111|1112|1111|1112|1113|1114|1115|1116|1117|2228|2229|2210|2211|2212|2221|2222|2223|2224|2225|2226|2227

Please help me out this issue if startm=&n will it display n value
Can we use-------. /script.sh 8 or. /script.sh oct. So that i cannt chage the script. Every time?

Last edited by Don Cragun; 10-27-2015 at 07:08 PM.. Reason: Fix QUOTE tags.
# 7  
Old 10-27-2015
Quote:
Originally Posted by Priti2277
Please help me out this issue if startm=&n will it display n value
Can we use-------. /script.sh 8 or. /script.sh oct. So that i cannt chage the script. Every time?
You could use the script I provided in post #5 using 3 character month name abbreviations, or you could put RudiC's 2nd awk command in a shell script as:
Code:
#!/bin/ksh
awk -F\| '
        {printf "%s|%s", $1, $2
         for (i=1; i<=24; i++) printf "|%s", $(3 + (STARTM-2+i)%12 + 12*int((i-1)/12))
         printf "\n"
        }
' STARTM="$1" file

and pass it a month number instead of an abbreviated month name.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

To pass one month range in sql script

Hi Guys, i am having .sql script which inserts data from one table to another table based on date condition, i need to pass range on based on how many number of months, for e.g set timing on; whenever sqlerror exit failure; spool myscript.log append accept start_date... (7 Replies)
Discussion started by: rohit_shinez
7 Replies

2. Shell Programming and Scripting

Need last month files after 10th of every month

Hi, I need all file names in a folder which has date >= 10th of last month, Example : files in folder AUTO_F1_20140610.TXT BUTO_F1_20140616.TXT CUTO_F1_20140603.TXT FA_AUTO_06012014.TXT LA_AUTO_06112014.TXT MA_AUTO_06212014.TXT ZA_AUTO_06232014.TXT Output: AUTO_F1_20140610.TXT... (9 Replies)
Discussion started by: nani1984
9 Replies

3. Shell Programming and Scripting

How to add decimal month to some month in sql, php, perl, bash, sh?

Hello, i`m looking for some way to add to some date an partial number of months, for example to 2015y 02m 27d + 2,54m i need to write this script in php or bash or sh or mysql or perl in normal time o unix time i`m asking or there are any simple way to add partial number of month to some... (14 Replies)
Discussion started by: bacarrdy
14 Replies

4. Shell Programming and Scripting

Convert From Month Number to Month Name

Hi, I have a script that accepts an input date from the user in yyyy-mm-dd format. I need to get the mm-dd part and convert it to month name. example: 2011-11-15 I want that to become "Nov 15" I don't have the GNU date, I am using an AIX os. Thanks. (1 Reply)
Discussion started by: erin00
1 Replies

5. Shell Programming and Scripting

How to pass current year and month in FOR LOOP in UNIX shell scripting?

Hi Team, I have created a script and using FOR LOOP like this and it is working fine. for Month in 201212 201301 201302 201303 do echo "Starting the statistics gathering of $Month partitions " done But in my scripts the " Month " variable is hard-coded. Can you please any one... (3 Replies)
Discussion started by: shoan
3 Replies

6. Linux

Pc Requirements

I am using a Pentium III and I want to change my cpu, which is better (AMD or dual core processor) for linux operating system. Thanks in advance for any help. (1 Reply)
Discussion started by: billcrosby
1 Replies

7. Shell Programming and Scripting

Script to counting a specific word in a logfile on each day of this month, last month etc

Hello All, I am trying to come up with a shell script to count a specific word in a logfile on each day of this month, last month and the month before. I need to produce this report and email it to customer. Any ideas would be appreciated! (5 Replies)
Discussion started by: pnara2
5 Replies

8. UNIX for Dummies Questions & Answers

Requirements for unix os

what are the requirements for unix os.i got now windows xp operating system.without disturbing windows xp i want to install unix os. (1 Reply)
Discussion started by: manohar12345678
1 Replies

9. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

10. Shell Programming and Scripting

Pass the first date and last date of previous month

Hi All, I need to run a job every month at the beginning of the month which is scheduled through autosys, lets say on 03/01/2010. I need to pass the last month's i.e February's first_date = 02/01/2010 and last_date = 02/28/2010 as variables to a stored procedure. Can somebody please pass... (2 Replies)
Discussion started by: vigdmab
2 Replies
Login or Register to Ask a Question